c# - Fluent mapping and relation table with Active flag -


i have relation table 1 additional column active bit not null

i fail understand how should map in ef6, have 3 tables, foo, bar , foobar foobar relation table

foobar {    fooid: int (key , fk)    barid: int (key , fk)    active: bit } 

foo entity

public class foo {    public int id { get;set; }      public icollection<foobar> bars { get; set; }     ... } 

foobar entity

public class foobar {    public foo foo { get;set; }      public bar bar { get;set; }      public bool active { get;set; } } 

and problem, ef configuration foobar config

public class foobarconfiguration : entitytypeconfiguration<foobar>     {         public foobarconfiguration()         {             haskey(fb => new[] {fb.foo.id, pv.bar.id}); //is correct?              property(pv => pv.active);              totable("productvehicle");         }     } 

i have no clue have foo config should like, im stuck, tried like

public class fooconfiguration : entitytypeconfiguration<foo> {     public fooconfiguration()     {         haskey(f => f.id);                    hasmany(f => f.bars)             .withrequired(f => p.foo)             .hasforeignkey(f => f.foo.id);          totable("foo");     } } 

i get

a first chance exception of type 'system.invalidoperationexception' occurred in entityframework.dll

additional information: properties expression 'f => f.foo.id' not valid. expression should represent property: c#: 't => t.myproperty' vb.net: 'function(t) t.myproperty'. when specifying multiple properties use anonymous type: c#: 't => new { t.myproperty1, t.myproperty2 }' vb.net: 'function(t) new { t.myproperty1, t.myproperty2 }'.

i change fooconfig

hasmany(f => p.bars)     .withrequired(fb => fb.foo)     .map(map => map.mapkey("fooid")); 

i little longer, fails on foobar config with

a first chance exception of type 'system.invalidoperationexception' occurred in entityframework.dll

additional information: properties expression 'fb => new [] {fb.foo.id, fb.bar.id}' not valid. expression should represent property: c#: 't => t.myproperty' vb.net: 'function(t) t.myproperty'. when specifying multiple properties use anonymous type: c#: 't => new { t.myproperty1, t.myproperty2 }' vb.net: 'function(t) new { t.myproperty1, t.myproperty2 }'.

edit: ended solution little more domaindriven,

public class productvehicle {     private product _product;     private vehicle _vehicle;     internal int productid { get; set; }     internal int vehicleid { get; set; }      public product product     {         { return _product; }         set         {             _product = value;             productid = value.id;         }     }      public vehicle vehicle     {         { return _vehicle; }         set         {             _vehicle = value;             vehicleid = value.id;         }     }      public bool active { get; set; } } 

you're trying use property of navigation property key or foreign key, isn't allowed. join table (and corresponding entity) should have columns (and properties) foreign keys , composite primary key

add foreign keys join table model serve composite primary key

public class foobar {    public int fooid { get; set; }    public int barid { get; set; }     public foo foo { get;set; }      public bar bar { get;set; }      public bool active { get;set; } } 

configure foreign keys primary keys

public class foobarconfiguration : entitytypeconfiguration<foobar> {     public foobarconfiguration()     {         haskey(fb => new[] { fb.fooid, fb.barid });          // reversed version placed in foo configuration         // 1 necessary         hasrequired(fb => fb.foo)             .withmany(fb => fb.bars)             .hasforeignkey(fb => fb.fooid);          // similar relationship bar          property(pv => pv.active);          totable("productvehicle"); // mean foobar right? ;)     } } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -