Cso*_*rgo 1 c# entity-framework entity-framework-core .net-core ef-core-2.0
我使用VSCode 创建了一个API .Net core
并EFCore
遵循本教程.
我有很多我的MySQL数据库的模型,因为我是"迁移"我EF6
跟asp.net WebAPI
到.Net core
,所以我只是复制粘贴,避免了大量的工作(再次).
当我尝试做一个简单Get
的事情时,EFCore连接两列不同的表:
控制器用户
[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();
}
}
Run Code Online (Sandbox Code Playgroud)
'用户'模型
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
}
Run Code Online (Sandbox Code Playgroud)
表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; }
}
Run Code Online (Sandbox Code Playgroud)
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()
Run Code Online (Sandbox Code Playgroud)
请注意,该字段u.account_typeid
确实不存在.这是一个串连account_type
的user
表id
的account_type
表.
为什么会这样?
非常感谢和抱歉我的英语
这在EF Core文档的关系 - 无外键属性部分中进行了解释:
如果未找到外键属性,则将引入带有名称的影子外键属性
<navigation property name> <主键属性名称>
您需要user.account_type
通过数据注释为导航属性指定FK属性:
[ForeignKey("id_account_type")]
public virtual account_type account_type { get; set; }
Run Code Online (Sandbox Code Playgroud)
或流利的API:
modelBuilder.Entity<user>()
.HasOne(e => e.account_type)
.WithMany(e => e.user)
.HasForeignKey(e => e.id_account_type);
Run Code Online (Sandbox Code Playgroud)