我有以下EF 6.1.3代码第一个POCO类:
[Table("Users", Schema = "CRM")]
public class User : SqlBaseModel
{
}
在SQL Server中生成以下表格:
当我像这样向模型添加外键属性时:
[Table("Users", Schema = "CRM")]
public class User : SqlBaseModel
{
public Guid? ContactId { get; set; }
[ForeignKey("ContactId")]
public virtual Contact Contact { get; set; }
}
和Contacts中的相应导航属性:
[Table("Contacts", Schema = "CRM")]
public class Contact : SqlBaseModel
{
public Guid? AccountId { get; set; }
[ForeignKey("AccountId")]
public virtual Account Account { get; set; }
public Guid? ParentContactId { get; set; }
[ForeignKey("ParentContactId")]
public virtual Contact ParentContact { get; set; }
public virtual List<User> Users { get; set; }
public virtual List<Contact> Contacts { get; set; }
public virtual List<Project> Projects { get; set; }
public virtual List<Case> Cases { get; set; }
}
它在SQL Server中生成以下表格:
如您所见,EF会生成2个字段:ContactId和Contact_Id。
有人可以建议我删除Contact_Id(带下划线)字段的必要条件吗? EF生成2个FK字段我做错了什么? 感谢