简体   繁体   中英

Entity Framework Fluent API configuration

Here is the thing bugging me, I have 2 entities --- 'Master' and 'Slave', let me list them first:

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;

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,

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.

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. 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'.

Because association behavior 'pertain to EntityObject and POCOs that create dyanmic proxies.' --- Julia Lerman's [Programming Entity Framework, Chapter 19].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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