[英]Entity Framework 7 Foreign Key Column Name
我有一个MasterUserApprovalOfficial实体,带有两个外键MasterUserId和SoftwareSystemId。 EF 7非常聪明,可以确定这两个属性是外键。这是我的MasterUserApprovalOfficial类
public class MasterUserApprovalOfficial
{
public int MasterUserApprovalOfficialId { get; set; }
public int MasterUserId { get; set; }
public MasterUser MasterUser { get; set; }
public int SofwareSystemId { get; set; }
public SoftwareSystem SoftwareSystem { get; set; }
public bool Active { get; set; }
public DateTime CreateDate { get; set; }
public MasterUserApprovalOfficial()
{
CreateDate = DateTime.Now;
}
}
如果我查看创建的表,则MasterUserId列已创建并按预期命名,但是SoftwareSystemId列却创建为SoftwareSystemSoftwareSystemId
(在主键名后附加的类名)
有什么理由吗?
public int SofwareSystemId { get; set; }
再次读取该属性。 有失踪:)
第二个答案也是正确的,您不需要外键的属性。 一个班级的成员就足够了
使用ForeignKey
和Column
属性可以解决此问题:
[ForeignKey("SoftwareSystem"),Column("SoftwareSystemId")]
public int SoftwareSystemId { get; set; }
默认情况下,外键将添加“ Id”,但是由于您已经拥有一个具有该名称的属性(并且EF显然无法识别为外键),因此将其更改为SoftwareSystemSoftwareSystemId
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.