EF 4.1 Code First 外键添加额外列
我有以下两个实体(在很多实体中,但这些给了我问题)
public class StartPoint
{
public int StartPointId { get; set; }
public string Description { get; set; }
public int StartPointNumber { get; set; }
public int StartAreaId { get; set; }
public StartArea StartArea { get; set; }
}
,
public class StartArea
{
public int StartAreaId { get; set; }
public string Description { get; set; }
public ICollection<StartPoint> StartPoints { get; set; }
}
当允许 EF 创建我的数据库时,它抱怨我需要关闭级联删除,因为有多个路径,我已经这样做了,如下所示。
modelBuilder.Entity<StartPoint>()
.HasRequired(x => x.StartArea)
.WithMany()
.HasForeignKey(x => x.StartAreaId)
.WillCascadeOnDelete(false);
问题是,创建数据库时我得到两个外键,一个称为 StartAreaId,如我所料,另一个是 StartArea_StartAreaId。仅使用 StartAreaId。为什么以及如何摆脱 StartArea_StartAreaId。如果我删除上下文中的 HasForeignKey 并从实体中删除 StartAreaId,我会得到多个 StartArea_StartAreaId 列,如 StartArea_StartAreaId 和 StartArea_StartAreaId1 所示。我需要做什么来阻止这种情况发生?我只想 StartAreaId 作为我的外键。
I have the following two entities (among many, but these give me the problem)
public class StartPoint
{
public int StartPointId { get; set; }
public string Description { get; set; }
public int StartPointNumber { get; set; }
public int StartAreaId { get; set; }
public StartArea StartArea { get; set; }
}
and
public class StartArea
{
public int StartAreaId { get; set; }
public string Description { get; set; }
public ICollection<StartPoint> StartPoints { get; set; }
}
When allowing EF to create my database it complained that I needed to turn off cascade delete because of multiple paths, which I have done as follows.
modelBuilder.Entity<StartPoint>()
.HasRequired(x => x.StartArea)
.WithMany()
.HasForeignKey(x => x.StartAreaId)
.WillCascadeOnDelete(false);
The problem is, when the database is created I get two foreign keys, one is called StartAreaId, as I would expect, and the other is StartArea_StartAreaId. Only StartAreaId is being used. Why and how do I get rid of the StartArea_StartAreaId. If I remove the HasForeignKey in the context and remove the StartAreaId from the entity I get multiple StartArea_StartAreaId columns, as in StartArea_StartAreaId and StartArea_StartAreaId1. What do I need to do to stop this from happening ? I just want StartAreaId as my foreign key.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是空的
WithMany
:The problem is empty
WithMany
: