简体   繁体   English

实体框架Fluent API配置

[英]Entity Framework Fluent API configuration

Here is the thing bugging me, I have 2 entities --- 'Master' and 'Slave', let me list them first: 这是困扰我的事情,我有2个实体-“ Master”和“ Slave”,让我首先列出它们:

public class Master
{
    public Guid Id {get;set;}
    public virtual ICollection<Slave> Slaves { get; set; }
    public virtual Slave ParentSlave { get; set; }
}

class Slave
{
    public Guid Id { get; set; }
    public Guid ParentMasterId { get; set; }
    public Master ParentMaster { get; set; }
    public Guid? ChildMasterId { get; set; }
    public Master ChildMaster { get; set; }
}

Basically, 基本上,

  1. Master have list of Slaves; 主人有奴隶名单;
  2. Slave should belong to a Master(Parent); 奴隶应属于主人(父母);
  3. Slave have another Master(Child) which then can have list of Slaves; 从站有另一个Master(Child),然后可以具有从站列表;

Here comes corresponding data mapping class, 这里是对应的数据映射类,

class MasterDataMap : EntityTypeConfiguration<Master>
{
    public MasterDataMap()
    {
        HasKey(i => i.Id);
        HasOptional(o => o.ParentSlave).WithRequired(o=>o.ChildMaster);
        ToTable("Master");
    }
}

class SlaveDataMap : EntityTypeConfiguration<Slave>
{
    public SlaveDataMap()
    {
        HasKey(i => i.Id);
        HasRequired(o => o.ParentMaster).WithMany(m => m.Slaves).HasForeignKey(k=>k.ParentMasterId).WillCascadeOnDelete(true);
        HasRequired(o => o.ChildMaster).WithOptional(d => d.ParentSlave);
        ToTable("Slave");
    }
}

Those 2 entities can pass the EF model validation without any problem, and tables can be created successfully during end-to-end test, after running following code, 这两个实体可以顺利通过EF模型验证,并且在运行以下代码后,可以在端到端测试期间成功创建表,

for (var idx = x; idx <= xx; idx++)
{
   topMaster.Slaves.Add(new Slave
                        {
                            Id = Guid.NewGuid(),
                            ChildMaster = new Master
                                            {
                                                Id = Guid.NewGuid(),
                                            }
                        });
}

topMaster and its slaves all been inserted into tables, childmaster(s) also been inserted, but they have not associated with topMaster's Slaves which means 'ChildMaster' are null and 'ChildMasterId' are empty guid. topMaster及其从属设备均已插入表中,还插入了childmaster,但它们未与topMaster的从属设备相关联,这意味着'ChildMaster'为null,而'ChildMasterId'为空guid。

I don't know what is wrong with data mapping classes to cause this issue. 我不知道导致此问题的数据映射类有什么问题。

/* / *

In case of fluent Entity one-to-one mapping with both side navigation, you cannot have custom foreign key name explicitly. 如果使用带有双向导航的流畅的实体一对一映射,则不能显式拥有自定义外键名称。 At least I do not know a way to do it until version EF 5.0. 至少在EF 5.0版之前,我不知道一种方法。 I know we can easily do it using attribute way . 我知道我们可以轻松地使用属性方式做到这一点

*/ * /

public class Master
    {
        public Guid Id {get;set;}
        public virtual ICollection<Slave> Slaves { get; set; }
        public virtual Slave ParentSlave { get; set; }
    }

class Slave
{
    public Guid Id { get; set; }
    public Guid ParentMasterId { get; set; }
    public Master ParentMaster { get; set; }
    public Master ChildMaster { get; set; }
}

class MasterDataMap : EntityTypeConfiguration<Master>
{
    public MasterDataMap()
    {
        HasKey(i => i.Id);
        //HasOptional(o => o.ParentSlave).WithRequired(o=>o.ChildMaster);
        ToTable("Master");
    }
}

class SlaveDataMap : EntityTypeConfiguration<Slave>
{
    public SlaveDataMap()
    {
        HasKey(i => i.Id);
        HasRequired(o => o.ParentMaster).WithMany(m => m.Slaves).HasForeignKey(k=>k.ParentMasterId).WillCascadeOnDelete(true);
        HasRequired(o => o.ChildMaster).WithOptional(d => d.ParentSlave);
        ToTable("Slave");
    }

Ok, guys, I figure out the solution. 好的,伙计们,我想出了解决方案。 That is purely my fault, I forget to add 'virtual' before 'ParentMaster' and 'ChildMaster'. 纯粹是我的错,我忘记在“ ParentMaster”和“ ChildMaster”之前添加“ virtual”。

Because association behavior 'pertain to EntityObject and POCOs that create dyanmic proxies.' 因为关联行为“与创建动态代理的EntityObject和POCO有关”。 --- Julia Lerman's [Programming Entity Framework, Chapter 19]. ---朱莉娅·莱曼(Julia Lerman)的[编程实体框架,第19章]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM