c# - Do you need to specify both ends of a relation in Entity Framework? -


i'm trying build asp.net mvc 4 application in vs2010 (.net 4.0) entity framework 5 (but understand not ef5-features supported in .net 4.0), using scaffolded repositories.

i have relation feel rather one-sided, event fits in timeslot. simplicity, wanted model property:

public class event {     public int id { get; set; }     public int timeslotid { get; set; }     [foreignkey("timeslotid")]     public timeslot timeslot { get; set; } }  public class timeslot {     public int id { get; set; }     public int starthour { get; set; }     //... more plain int properties } 

however, can't seem work @ all! when prepopulate timeslots table in database, try following:

var eventrep = new eventrepository(); var timeslotrep = new timeslotrepository(); var times = timeslotrep.all.getenumerator(); times.movenext();  var = new event { timeslot = times.current }; eventrep.insertorupdate(a);  eventrep.save(); timeslotrep.save(); 

i.e. timeslot database, assign event, , save everything. however, when try save timeslotrepository, exception:

the property 'id' part of object's key information , cannot modified. 

does have explanation why attempts edit timeslot.id property?
problem relate fact timeslotdoesn't have list of events?
need both ends of relation in ef have property/list of opposite end?


Comments