实体框架关系未从数据库加载 [英] Entity Framework relation not loaded from database
问题描述
我正在使用Entity Framework 6 Code First,并且遇到了多对多关系未正确同步到我的内部对象的问题。
I'm playing around with Entity Framework 6 Code First and have the problem that a many-to-many relationship is not correctly synchronized to my "internal" objects.
我有以下课程:
public class ClassA
{
public String AId { get; set; }
public ICollection<ClassB> Bs { get; set; }
public ICollection<ClassC> Cs { get; set; }
}
public class ClassB
{
public int BId { get; set; }
ICollection<ClassA> As { get; set; }
}
public class ClassC
{
public int CId { get; set; }
public ClassA A { get; set; }
}
A和B之间存在多对多关系,其中A和C有一对多的关系。我使用Fluent API构建映射:
A and B have a many-to-many relationship where A and C have a one-to-many relationship. I use Fluent API to build the mappings:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ClassA>().ToTable("ClassA");
modelBuilder.Entity<ClassA>().HasKey(a => a.AId);
modelBuilder.Entity<ClassA>().HasManyClassB>(a => a.Bs).WithMany(b => b.As).Map(
a =>
{
a.MapLeftKey("AId");
a.MapRightKey("BId");
a.ToTable("ClassAClassB");
});
modelBuilder.Entity<ClassA>().Property(a => a.OrderId).IsVariableLength().HasColumnName("AId");
modelBuilder.Entity<ClassB>().ToTable("ClassB");
modelBuilder.Entity<ClassB>().HasKey(b => b.BId);
modelBuilder.Entity<ClassB>().Property(b => b.BId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.HasColumnName("BId");
modelBuilder.Entity<ClassC>().ToTable("ClassC");
modelBuilder.Entity<ClassC>().HasKey(c => c.CId);
modelBuilder.Entity<ClassC>().HasRequired(c => c.A).WithMany(a => a.Bs).Map(m => m.MapKey("AId"));
modelBuilder.Entity<ClassC>().Property(c => c.CId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.HasColumnName("CId");
}
如果我创建ClassA,ClassB和ClassC类的对象,请将它们添加到我的数据库上下文并调用SaveChanges()。在我看来,值已正确存储在数据库中。
If i create objects of class ClassA, ClassB and ClassC, add them to my database context and invoke SaveChanges(). In my opinion the values are stored in the database correctly:
MariaDB [Test]> select * from ClassA;
+----------+
| BId |
+----------+
| 2017-002 |
+----------+
MariaDB [Test]> select * from ClassB;
+--------+
| BId |
+--------+
| 2 |
+--------+
MariaDB [OMSData]> select * from ClassAClassB;
+----------+-----+
| AId | BId |
+----------+-----+
| 2017-002 | 2 |
+----------+-----+
MariaDB [Test]> select * from classC;
+----+-----------+
| CId | AId |
+-----+----------+
| 1 | 2017-002 |
+-----+----------+
但是,如果我再次执行测试程序,关系将无法正确恢复。
However, if I execute my test program another time, the relationships are not correctly restored.
这意味着:
- ClassA中的B集合不包含任何对象(反之亦然)。
- 我只能通过ClassA访问ClassC的对象
我是否必须手动调用同步,或者模型定义中有错误?
Do I have to invoke the synchronization manually or is there an error in model definition?
预先感谢!
推荐答案
您的课程不是正确的。从逻辑上考虑,您需要将多对多绑定存储在某个地方,这意味着您需要存储多对多关系的第三个实体类。
You are not building your classes right. Think logically, you need to store your many to many bindings somewhere, meaning you need a third entity class which stores the many-to-many relations.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
进行如下课程:
public class ClassA
{
[Index(IsUnique = true)]
[MaxLength(200)]
public String AId { get; set; }
public virtual List<AB> AB { get; set; }
public List<ClassC> C { get; set; }
}
public class ClassB
{
public int BId { get; set; }
public virtual List<AB> AB { get; set; }
}
public class AB
{
public int Id { get; set; }
[Required]
public virtual A A { get; set; }
[Required]
public virtual B B { get; set; }
[MaxLength(200)]
public string AId { get; set; }
public int BId { get; set; }
}
public class ClassC
{
public int CId { get; set; }
public ClassA A { get; set; }
public int AId { get; set; }
}
创建代码优先的迁移,实体框架将对此有所了解并勾画出轮廓您的数据库相应。仅将A和B的ID存储在第三个实体AB中。
Create a code-first migration and entity framework will understand this and sketch out your database accordingly. Store just the IDs of A and B in the third entity AB.
这篇关于实体框架关系未从数据库加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!