EFCore选择了错误的列名

3
我已经使用VSCode按照this教程,使用.Net coreEFCore创建了一个API。
我的MySQL数据库有很多模型,因为我正在将asp.net WebAPIEF6迁移到.Net core,所以我只是复制并粘贴以避免重复劳动。
当我尝试进行简单的Get时,EFCore会连接两个不同表的两列: User控制器
[Route("v1/[controller]")]
public class UserController : Controller
{

    private readonly IntentContext _context;

    public UserController(IntentContext context)
    {
        _context = context;
    }

    [HttpGet]
    public IEnumerable<user> GetAll()
    {
        return _context.user.ToList();
    }
}

“用户”模型

public class user
{
    public user()
    {
        user_image = new HashSet<user_image>();
        user_credit_card = new HashSet<user_credit_card>();
        user_pocket_history = new HashSet<user_pocket_history>();
        user_pocket = new HashSet<user_pocket>();

        //A lot of table instances
    }

    public string id { get; set; }
    public string username { get; set; }
    public string password { get; set; }
    public string id_account_type { get; set; }
    public int user_status { get; set; }
    public DateTime create_time { get; set; }
    public DateTime update_time { get; set; }

    public virtual account_type account_type { get; set; }

    public virtual ICollection<user_image> user_image { get; set; }
    public virtual ICollection<user_credit_card> user_credit_card { get; set; }
    public virtual ICollection<user_pocket_history> user_pocket_history { get; set; }
    public virtual ICollection<user_pocket> user_pocket { get; set; }

    //A lot of table relations
}

表 account_type

public class account_type
{
    public account_type()
    {
        this.user = new HashSet<user>();
        this.establishment_employee = new HashSet<establishment_employee>();
    }

    public string id { get; set; }
    public string account_description { get; set; }
    public string account_name { get; set; }
    public DateTime create_time { get; set; }
    public DateTime update_time { get; set; }

    public virtual ICollection<user> user { get; set; }
    public virtual ICollection<establishment_employee> establishment_employee { get; set; }
}

在 GET 请求期间的终端日志

fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
  Failed executing DbCommand (140ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
  SELECT `u`.`id`, `u`.`Id_account_type`, `u`.`account_typeid`, `u`.`create_time`, `u`.`password`, `u`.`update_time`, `u`.`user_status`, `u`.`username`
  FROM `user` AS `u`
MySql.Data.MySqlClient.MySqlException (0x80004005): Unknown column 'u.account_typeid' in 'field list' ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Unknown column **'u.account_typeid'** in 'field list'
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

//A lot of exceptions generated 

fail: Microsoft.EntityFrameworkCore.Query[10100]
  An exception occurred in the database while iterating the results of a query for context type 'IntentAPI.Models.IntentContext'.
  MySql.Data.MySqlClient.MySqlException (0x80004005): Unknown column 'u.account_typeid' in 'field list' ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Unknown column 'u.account_typeid' in 'field list'
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

请注意,字段u.account_typeid实际上不存在。它是用户表的account_typeaccount_type表的id连接而成。
为什么会出现这种情况呢?
非常感谢,对我的糟糕英语表示抱歉。
1个回答

8
这在EF Core文档的“关系-无外键属性”部分中有解释:https://learn.microsoft.com/en-us/ef/core/modeling/relationships#no-foreign-key-property 如果没有找到外键属性,将引入一个影子外键属性,其名称为<导航属性名称><主键属性名称> 您需要通过数据注释来指定user.account_type导航属性的FK属性。
[ForeignKey("id_account_type")]
public virtual account_type account_type { get; set; }

或者流畅的API:
modelBuilder.Entity<user>()
    .HasOne(e => e.account_type)
    .WithMany(e => e.user)
    .HasForeignKey(e => e.id_account_type);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接