gpt4 book ai didi

c# - EFCore 选择了错误的列名

转载 作者:太空宇宙 更新时间:2023-11-03 19:43:07 26 4
gpt4 key购买 nike

我已经在 this 之后使用 .Net coreEFCore 创建了一个 API使用 VSCode 的教程。

我的 MySQL 数据库有很多模型,因为我正在“迁移”我的 EF6asp.net WebAPI.Net core,所以我只是复制并粘贴以避免大量工作(再次)。

当我尝试执行简单的 Get 时,EFCore 正在连接不同表的两列:

Controller 用户

[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 确实不存在。它是 user 表的 account_typeaccount_type 表的 id 的串联。

为什么会这样?

非常感谢和抱歉我糟糕的英语

最佳答案

这在 Relationships - No Foreign Key Property 中有解释EF Core 文档部分:

If no foreign key property is found, a shadow foreign key property will be introduced with the name

<navigation property name><principal key property name>

您需要通过任一数据注释为 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);

关于c# - EFCore 选择了错误的列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50054611/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com