EF 4.1 Code First 外键添加额外列

发布于 2024-11-17 16:13:43 字数 1113 浏览 2 评论 0原文

我有以下两个实体(在很多实体中,但这些给了我问题)

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨落□心尘 2024-11-24 16:13:43

问题是空的WithMany

    modelBuilder.Entity<StartPoint>()
        .HasRequired(x => x.StartArea)
        .WithMany(y => y.StartPoints)
        .HasForeignKey(x => x.StartAreaId)
        .WillCascadeOnDelete(false);

The problem is empty WithMany:

    modelBuilder.Entity<StartPoint>()
        .HasRequired(x => x.StartArea)
        .WithMany(y => y.StartPoints)
        .HasForeignKey(x => x.StartAreaId)
        .WillCascadeOnDelete(false);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文