【发布时间】:2019-02-14 08:19:28
【问题描述】:
我有 Issue 实体有 2 个字段作为 FK 到 User 实体,如下所示。尽管我正确配置了属性以防止创建额外的Xxx_Id 字段而不是PropertyNameId,但除了PropertyNameId 字段之外,表中还创建了一个Xxx_Id 字段。在此示例中,除了 AssigneeId 和 ReporterId 字段之外,Issue 表中还创建了 User_Id 字段。这里有什么错误吗? User 实体中的 HashSet 是否会导致这个额外的 User_Id 字段?
问题:
public class Issue : BaseEntity
{
[Key]
public int Id { get; set; }
//Foreign key for Reporter (User entity)
public short ReporterId { get; set; }
//Foreign key for Assignee (User entity)
public short AssigneeId { get; set; }
[ForeignKey("ReporterId")]
public virtual User Reporter { get; set; }
[ForeignKey("AssigneeId")]
public virtual User Assignee { get; set; }
}
用户:
public class User : BaseEntity
{
[Key]
public short Id { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
//code omitted for brevity
public User()
{
Issues = new HashSet<Issue>();
//code omitted for brevity
}
public virtual ICollection<Issue> Issues { get; set; }
//code omitted for brevity
}
以下是迁移文件中的相关行:
CreateTable(
"Issue",
c => new
{
Id = c.Int(nullable: false, identity: true),
ReporterId = c.Short(nullable: false),
AssigneeId = c.Short(nullable: false),
User_Id = c.Short() //???
})
.PrimaryKey(t => t.Id)
.ForeignKey("com.User", t => t.User_Id) //???
.ForeignKey("com.User", t => t.AssigneeId)
.ForeignKey("com.User", t => t.ReporterId)
.Index(t => t.ReporterId)
.Index(t => t.AssigneeId)
.Index(t => t.User_Id); //???
【问题讨论】:
-
可以发
BaseEntity类的内容吗? -
有你的问题!我正在彻底回答。
-
@NguyenThanh 我添加了,谢谢...
-
@TanvirArjel 非常感谢,我正在等待您的回复...
标签: c# sql-server asp.net-mvc entity-framework ef-code-first