Issue with Entity Framework TPT hierarchy and relationships -


my current ef6 implementation goes this:

    public class parent     {         public int id { get; set; }         public virtual string name { get; set; }         public string description { get; set; }         public virtual icollection children { get; set; }          public int otherentityid { get; set; }         public virtual otherentity otherentity { get; set; }     }      public class child : parent     {         public int id { get; set; }         public override string name          {              { return parent == null ? "dummy" : parent.name; }         }         public string description { get; set; }         public parentid { get; set; }         public virtual parent { get; set; }          public override int otherentityid         {             { return parent == null ? default(int) : parent.otherentityid; }         }     } 

the fluent configuration child goes this:

    this.totable("child")         .hasrequired(x => x.parent)         .withmany(x => x.children)         .hasforeignkey(xy => xy.parentid); 

the idea have 1 entry every parent , child in parent table. yet, child entity should not have name of own, name it's parent. need way navigate every child parent. , need way navigate every parent children. goes on 1 level.

the reason implementation due requirement values parent, yet child must have same public interface parent. keep in mind can't change parent here, part of framework have stick with. child part of implementation.

the problem seems change tracking. if query child, ef loads child first, checks properties, , loads parent second. since name different, ef name property changed, though have not changed manually.

the solution works somewhat, long load entities without tracking. moment try load them tracking enabled exception:

a referential integrity constraint violation occurred: primary key property part of referential integrity constraint cannot changed when dependent object unchanged unless being set association's principal object. principal object must tracked , not marked deletion.

same happens if try attach entity after loading, , set state unchanged. not change primary keys, or keys @ all, not clear me, why error happens. seems error happens during relationship fixup. fear "otherentityid" can't verify it, since have no idea how debug it. has ideas problem might or how can debug properly?

i tried internal values like: https://www.exceptionnotfound.net/entity-change-tracking-using-dbcontext-in-entity-framework-6/

changetracker.entries().where(p => p.state == entitystate.modified).tolist();

also seams force relationship fixup leads error.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -