c# - How can I map this navigation property? -


i have 2 collections. parent has many children, stores current child's id.

public class foo  {     public int id { get; set; }     public icollection<bar> bars { get; set; }      public int? currentbarid { get; set; }      // adding causes error below     public virtual currentbar currentbar { get; set; }  }  public class bar  {     public int id { get; set; }     public int fooid { get; set; }     public virtual foo foo { get; set; } } 

when add currentbarid property persisted correctly. however, when add currentbar property exception thrown when bar created.

i exception:

{"invalid column name 'foo_id'."} 

how can map navigation property in context?

update

i have played around bit , ended with:

modelbuilder.entity<foo>()     .hasoptional(x => x.currentbar)     .withoptionaldependent()     .map(map =>     {         map.totable("bar").mapkey("currentbarid");     }); 

now following error. in right direction, or how should trying this?

error 3034: problem in mapping fragments starting @ lines 213, 442:an entity 1 entityset mapped row mapped entity entityset possibly different key. ensure these 2 mapping fragments not map 2 unrelated entitysets 2 overlapping groups of rows.

assuming foo.bars , bar.foo ends of same relationship , currentbar 1 end of second relationship, try this:

modelbuilder.entity<foo>()     .hasmany(f => f.bars)     .withrequired(b => b.foo)     .hasforeignkey(b => b.fooid)     .willcascadeondelete(false);  modelbuilder.entity<foo>()     .hasoptional(f => f.currentbar)     .withmany()     .hasforeignkey(f => f.currentbarid)     .willcascadeondelete(false); 

this create (nullable) currentbarid fk column in foos table , (not nullable) fooid fk column in bars table.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -