c# - Entity Framework 6 mapping issues -


i've been using ef while, basic normalized tables, , little-to-no relationships between them. i'm looking branch out , running mapping issues, i'm sure solved simple onmodelcreating() changes, or additional model properties.

i'm creating website manage events can occur @ various locations. each event entity. each event has basic primitive properties, has virtual icollection<timeslot>.

    public virtual icollection<timeslot> timeslots     {         { return mtimeslots ?? (mtimeslots = new collection<timeslot>()); }         set { mtimeslots = value; }     } 

timeslot pretty simple too, it's supposed represent container collection of activities occur @ particular time.

public class timeslot {     private icollection<timeslotitem> mitems;      public virtual icollection<timeslotitem> items     {         { return mitems ?? (mitems = new collection<timeslotitem>()); }         set { mitems = value; }     }      [key]     public virtual int id { get; set; }      public virtual string label { get; set; } } 

since ef can't map collections of primitive type (string in case), created entity called timeslotitem, string entity mapping.

public class timeslotitem {     [key]     public virtual int id { get; set; }      public virtual string description { get; set; } } 

my issue how map together. ef doesn't map these correctly default, when seed database events, timeslots, , timeslotitems, maps 1 of events (the first one), , none of others. don't think foreign keys setup map correctly. it's not doing many-to-many @ moment, should, @ least believe.

my mapping is:

    protected override void onmodelcreating(dbmodelbuilder modelbuilder)     {         modelbuilder.entity<faroevent>()             .hasmany(f => f.timeslots);         modelbuilder.entity<timeslot>()             .hasmany(f => f.items);     } 

lazy loading enabled in ctor.

my seed is:

    protected override void seed(myeventsdatacontext context)     {         //  method called after migrating latest version.          var timeslotitems = new list<timeslotitem>         {             new timeslotitem {description = "do stuff 1"},             new timeslotitem {description = "do stuff 2"},             new timeslotitem {description = "do stuff 3"},         };         timeslotitems.foreach(t => context.timeslotitems.addorupdate(i => i.description, t));         context.savechanges();          var timeslots = new list<timeslot>         {             new timeslot             {                 label = "slot 1",                 items = new collection<timeslotitem> {timeslotitems[0], timeslotitems[1], timeslotitems[2]}             },             new timeslot             {                 label = "slot 2",                 items = new collection<timeslotitem> {timeslotitems[0], timeslotitems[1], timeslotitems[2]}             },             new timeslot             {                 label = "slot 3",                 items = new collection<timeslotitem> {timeslotitems[0], timeslotitems[1], timeslotitems[2]}             },         };         timeslots.foreach(t => context.timeslots.addorupdate(i => i.label, t));         context.savechanges();          var events = new list<myevent>         {             new myevent             {                 address = "123 street ln",                 campaignid = "abc123",                 city = "city",                 createddate = datetime.now,                 eventdate = datetime.now,                 eventtype = "tradeshow",                 name = "show name",                 productinterest = "myarm",                 state = "state",                 zipcode = "12345",                 timeslots = new collection<timeslot> {timeslots[0], timeslots[1], timeslots[2]}             },             new myevent             {                 address = "123 street ln",                 campaignid = "abc123",                 city = "city",                 createddate = datetime.now,                 eventdate = datetime.now,                 eventtype = "tradeshow",                 name = "show name",                 productinterest = "myarm",                 state = "state",                 zipcode = "12345",                 timeslots = new collection<timeslot> {timeslots[0], timeslots[1], timeslots[2]}             },             new myevent             {                 address = "123 street ln",                 campaignid = "abc123",                 city = "city",                 createddate = datetime.now,                 eventdate = datetime.now,                 eventtype = "tradeshow",                 name = "show name",                 productinterest = "myarm",                 state = "state",                 zipcode = "12345",                 timeslots = new collection<timeslot> {timeslots[0], timeslots[1], timeslots[2]}             },         };         events.foreach(t => context.myevents.addorupdate(i => i.name, t));         context.savechanges();     } 

try this:

modelbuilder.entity<faroevent>().hasmany(f => f.timeslots).withmany(f => f.items); 

you can find better explanation here: http://msdn.microsoft.com/en-us/data/jj591620.aspx#manytomany


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -