gpt4 book ai didi

entity-framework - EF4.1 代码第一对多自引用

转载 作者:行者123 更新时间:2023-12-05 01:38:14 25 4
gpt4 key购买 nike

我试图首先使用 EF4.1 代码进行一对多的自我引用。我的实体看起来像这样

public class Site
{
public int Id { get; set; }
public int? ParentId { get; set; }
public string Sitename { get; set; }

public virtual Site ChildSite { get; set; }
public virtual ICollection<Site> ChildSites { get; set; }
}

在我的上下文类中,我这样做是为了进行自我引用
modelBuilder.Entity<Site>()
.HasOptional(s => s.ChildSite)
.WithMany(s => s.ChildSites)
.HasForeignKey(s => s.ParentId);

但是当我尝试使用此代码将一些虚拟数据添加到我的数据库时
var sites = new List<Site>
{
new Site { ParentId = 0, Sitename = "Top 1" },
new Site { ParentId = 0, Sitename = "Top 2" },
new Site { ParentId = 0, Sitename = "Top 3" },
new Site { ParentId = 0, Sitename = "Top 4" },
new Site { ParentId = 1, Sitename = "Sub 1_5" },
new Site { ParentId = 1, Sitename = "Sub 1_6" },
new Site { ParentId = 1, Sitename = "Sub 1_7" },
new Site { ParentId = 1, Sitename = "Sub 1_8" },
new Site { ParentId = 2, Sitename = "Sub 2_9" },
new Site { ParentId = 2, Sitename = "Sub 2_10" },
new Site { ParentId = 2, Sitename = "Sub 2_11" },
new Site { ParentId = 2, Sitename = "Sub 2_12" },
new Site { ParentId = 3, Sitename = "Sub 3_13" },
new Site { ParentId = 3, Sitename = "Sub 3_14" },
new Site { ParentId = 3, Sitename = "Sub 3_15" },
new Site { ParentId = 3, Sitename = "Sub 3_16" },
new Site { ParentId = 4, Sitename = "Sub 4_17" },
new Site { ParentId = 4, Sitename = "Sub 4_18" },
new Site { ParentId = 4, Sitename = "Sub 4_19" },
new Site { ParentId = 4, Sitename = "Sub 4_20" }
};
sites.ForEach(s => context.Sites.Add(s));
context.SaveChanges();

我收到此错误:

Unable to determine the principal end of the 'Cms.Model.Site_ChildSite' relationship. Multiple added entities may have the same primary key.



我在这里错过了什么?

编辑:

这是我的问题的解决方案。我删除了 public virtual Site ChildSite { get; set; }从实体
public class Site
{
public int Id { get; set; }
public int? ParentId { get; set; }
public string Sitename { get; set; }

public virtual ICollection<Site> ChildSites { get; set; }
}

然后我更改了 modelBuilder对此
modelBuilder.Entity<Site>()
.HasMany(s => s.ChildSites)
.WithOptional()
.HasForeignKey(s => s.ParentId);

由于似乎数据库的自动生成不起作用,我继续像这样自己创建了表
int Id, not null, primary key
int ParentId, null
string Sitename, null

一切都如我所愿。

第二次编辑

以及让虚拟数据自动生成工作的最后一步
var parent1 = new Site
{
Sitename = "Top 1",
ChildSites = new List<Site>
{
new Site {Sitename = "Sub 1_5"},
new Site {Sitename = "Sub 1_6"},
new Site {Sitename = "Sub 1_7"},
new Site {Sitename = "Sub 1_8"}
}
};

ect . . .

context.Sites.Add(parent1);
context.SaveChanges();

请参阅下面的 Slauma 答案

最佳答案

我有一个不同的模型,但我会发布我的案例中必要的东西,它在 MVC 3 上与 EF4.1 一起工作得很好:

型号

public class Page
{
public int Id { get; set; }
public virtual string Title { get; set; }

public int? ParentId { get; set; }
public virtual Page Parent { get; set; }
public virtual ICollection<Page> Children { get; set; }
}

数据库 (使用流畅的API)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Page>()
.HasOptional(s => s.Parent)
.WithMany(s => s.Children)
.HasForeignKey(s => s.ParentId);
}

初始化程序
var page1 = new Page()
{
Title = "title"
};

context.Pages.Add(page1);

var page2 = new Page()
{
Parent = page1,
Title = "title2",
};

context.Pages.Add(page2);

关于entity-framework - EF4.1 代码第一对多自引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7509468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com