[英]How to set up many to many relationship correctly with EF6?
I have some issues with getting my many to many relationship to work.我在让我的多对多关系正常工作方面遇到了一些问题。 I have two tables, Beestje and Accessoire, and I want to show its contents.我有两个表,Beestje 和 Accessoire,我想显示其内容。
This are my tables: Beestje:这是我的桌子:Beestje:
public class Beestje
{
public Beestje()
{
this.Accessoires = new HashSet<Accessoire>();
}
[Key]
public int Id { get; set; }
public String Naam { get; set; }
public String Type { get; set; }
public int Prijs { get; set; }
public String Afbeelding { get; set; }
public virtual ICollection<Accessoire> Accessoires { get; set; }
}
Accessoire:配件:
public class Accessoire
{
public Accessoire()
{
this.Beestjes = new HashSet<Beestje>();
}
[Key]
public int Id { get; set; }
public String Naam { get; set; }
public double Prijs { get; set; }
public String Afbeelding { get; set; }
public virtual ICollection<Beestje> Beestjes { get; set; }
}
In my controller I retrieve the contents from the Beestje table and return it to the view:在我的控制器中,我从 Beestje 表中检索内容并将其返回到视图:
public ActionResult Beestjes()
{
ViewBag.Message = "Your application beestjes page";
List<Beestje> beestjes;
using (var context = new MyContext())
{
beestjes = context.Beestjes.ToList();
return View(beestjes);
}
}
When I print it like this there are no problems:当我这样打印时没有问题:
@model IEnumerable<BeestjeOpJeFeestje.Models.Beestje>
<h2>@ViewBag.Message</h2>
<ul>
@foreach (var a in Model)
{
<li>@a.Naam</li>
}
</ul>
But when I print it like this I get an error:但是当我这样打印时,我得到了一个错误:
@model IEnumerable<BeestjeOpJeFeestje.Models.Beestje>
<h2>@ViewBag.Message</h2>
<ul>
@foreach (var a in Model)
{
<li>@a.Naam</li>
foreach (var b in a.Accessoires)
{
<li>@b.Naam</li>
}
}
</ul>
It goes wrong at the part where I call the a.Accessoires.在我称之为 a.Accessoires 的部分出错了。 The error is: "System.ObjectDisposedException: 'The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.'"错误是:“System.ObjectDisposedException:‘ObjectContext 实例已被释放,不能再用于需要连接的操作。’”
Does someone know the correct way, and would be willing to provide me the right code, or give me some directions?有人知道正确的方法,愿意为我提供正确的代码,或给我一些指导吗?
Thanks in advance.提前致谢。
EDIT:编辑:
MyContext MyContext
public class MyContext : DbContext
{
public MyContext() : base("name=Local")
{
Database.SetInitializer(new MyContextInitializer());
}
public DbSet<Beestje> Beestjes { get; set; }
public DbSet<Accessoire> Accessoires { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
MyContextInitializer MyContextInitializer
public class MyContextInitializer : DropCreateDatabaseAlways<MyContext>
{
protected override void Seed(MyContext context)
{
base.Seed(context);
context.Beestjes.Add(new Beestje() { Id = 1, Naam = "Aap", Type = "Jungle", Prijs = 150, Afbeelding = "Content/images/aap.png" });
context.Beestjes.Add(new Beestje() { Id = 2, Naam = "Olifant", Type = "Jungle", Prijs = 250, Afbeelding = "Content/images/olifant.png" });
context.Beestjes.Add(new Beestje() { Id = 3, Naam = "Zebra", Type = "Jungle", Prijs = 200, Afbeelding = "Content/images/zebra.png" });
context.Beestjes.Add(new Beestje() { Id = 4, Naam = "Leeuw", Type = "Jungle", Prijs = 250, Afbeelding = "Content/images/leeuw.png" });
context.Beestjes.Add(new Beestje() { Id = 5, Naam = "Hond", Type = "Boerderij", Prijs = 75, Afbeelding = "Content/images/doggo.png" });
context.Beestjes.Add(new Beestje() { Id = 6, Naam = "Ezel", Type = "Boerderij", Prijs = 150, Afbeelding = "Content/images/donkey.png" });
context.Beestjes.Add(new Beestje() { Id = 7, Naam = "Koe", Type = "Boerderij", Prijs = 100, Afbeelding = "Content/images/koe.png" });
context.Beestjes.Add(new Beestje() { Id = 8, Naam = "Eend", Type = "Boerderij", Prijs = 50, Afbeelding = "Content/images/duck.png" });
context.Beestjes.Add(new Beestje() { Id = 9, Naam = "Kuiken", Type = "Jungle", Prijs = 35, Afbeelding = "Content/images/kuiken.png" });
context.Beestjes.Add(new Beestje() { Id = 10, Naam = "Pinguin", Type = "Sneeuw", Prijs = 175, Afbeelding = "Content/images/pingwing.png" });
context.Beestjes.Add(new Beestje() { Id = 11, Naam = "IJsbeer", Type = "Sneeuw", Prijs = 300, Afbeelding = "Content/images/ijsbeer.png" });
context.Beestjes.Add(new Beestje() { Id = 12, Naam = "Zeehond", Type = "Sneeuw", Prijs = 200, Afbeelding = "Content/images/zeehond.png" });
context.Beestjes.Add(new Beestje() { Id = 13, Naam = "Kameel", Type = "Woestijn", Prijs = 175, Afbeelding = "Content/images/kameel.png" });
context.Beestjes.Add(new Beestje() { Id = 14, Naam = "Slang", Type = "Sneeuw", Prijs = 125, Afbeelding = "Content/images/slang.png" });
var accessoire1 = new Accessoire() { Naam = "Banaan", Prijs = 0.50 };
accessoire1.Beestjes.Add(context.Beestjes.Find(1));
context.Accessoires.Add(accessoire1);
var accessoire2 = new Accessoire() { Naam = "Zadel", Prijs = 4.50 };
accessoire2.Beestjes.Add(context.Beestjes.Find(3));
context.Accessoires.Add(accessoire2);
var accessoire3 = new Accessoire() { Naam = "Krukje", Prijs = 5 };
accessoire3.Beestjes.Add(context.Beestjes.Find(4));
context.Accessoires.Add(accessoire3);
var accessoire4 = new Accessoire() { Naam = "Zweep", Prijs = 2.50 };
accessoire4.Beestjes.Add(context.Beestjes.Find(4));
context.Accessoires.Add(accessoire4);
var accessoire5 = new Accessoire() { Naam = "Bal", Prijs = 2.50 };
accessoire5.Beestjes.Add(context.Beestjes.Find(5));
accessoire5.Beestjes.Add(context.Beestjes.Find(12));
context.Accessoires.Add(accessoire5);
var accessoire6 = new Accessoire() { Naam = "Dansschoenen", Prijs = 3.00 };
accessoire6.Beestjes.Add(context.Beestjes.Find(10));
context.Accessoires.Add(accessoire6);
context.SaveChanges();
}
}
This is most likely caused by the fact that for each Beestje
, the Accessoires
property does not get loaded until you try to access it in the View, and at that moment the MyContext
has already been disposed.这很可能是由于对于每个Beestje
, Accessoires
属性在您尝试在视图中访问它之前不会加载,而此时MyContext
已经被释放。
The solution is to tell EF to load them immediately by using .Include()
, which is known as 'eager loading':解决方案是使用.Include()
告诉 EF 立即加载它们,这被称为“预先加载”:
using (var context = new MyContext())
{
beestjes = context.Beestjes
.Include("Accessoires")
.ToList();
return View(beestjes);
}
For reference, see https://learn.microsoft.com/en-us/ef/ef6/querying/related-data有关参考,请参阅https://learn.microsoft.com/en-us/ef/ef6/querying/related-data
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.