0

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.

ocuenca
  • 38,548
  • 11
  • 89
  • 102
wxdtony
  • 1
  • 1

2 Answers2

0

/*

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");
    }
pjobs
  • 1,247
  • 12
  • 14
  • Allow me to clarify it a little bit. It is EF 6.x, so it can be done. The source code can be compiled without any problem. It can also run from start to end. It also passed EF's data model validation. The only problem is association between 'ChildMaster' and 'Slave' cannot be established. – wxdtony Apr 22 '15 at 06:10
  • I try not to apply attributes to my POCO, but I will give it try if I can not find other solution. – wxdtony Apr 22 '15 at 17:39
0

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

wxdtony
  • 1
  • 1