entity framework - Code First CTP5 Many To Many Binding -
i have been using code first approach entity framework. have event class, band class , eventbands class maps many many relationship. code first approach worked fine (when didn't have eventbands class) decided wanted many many table store additional values. error message:
system.data.edm.edmentitytype: : entitytype 'eventbands' has no key defined. define key entitytype.
system.data.edm.edmentityset: entitytype: entityset eventbands based on type eventbands has no keys defined.
it obvious error message means. resolution isn't obvious. think have override model binding method not entirely sure how map keys approach.
any appreciated, have included classes in question below.
thanks in advance,
jon
event:
#region properties private int eventid; public int eventid { { return eventid; } set { eventid = value; } } private string name; public string name { { return name; } set { name = value; } } private string description; public string description { { return description; } set { description = value; } } private datetime startdatetime; public datetime startdatetime { { return startdatetime; } set { startdatetime = value; } } private datetime enddatetime; public datetime enddatetime { { return enddatetime; } set { enddatetime = value; } } private int venueuserid; public int venueuserid { { return venueuserid; } set { venueuserid = value; } } public virtual venue venue { get; set; } public virtual icollection<eventreview> reviews { get; set; } public virtual icollection<eventbands> eventbands { get; set; } public virtual icollection<fan> attendees { get; set; } #endregion #region constructor public event() { eventbands = new hashset<eventbands>(); attendees = new hashset<fan>(); startdatetime = datetime.now; enddatetime = datetime.now.adddays(14); } #endregion band:
public class band : postableuser { #region properties private int genregenreid; public int genregenreid { { return genregenreid; } set { genregenreid = value; } } public virtual genre genre { get; set; } public virtual icollection<album> albums { get; set; } public virtual icollection<bandreview> reviews { get; set; } public virtual icollection<eventbands> eventbands { get; set; } #endregion #region constructor public band() { eventbands = new hashset<eventbands>(); } #endregion } eventbands
#region properties private int eventeventid; public int eventeventid { { return eventeventid; } set { eventeventid = value; } } public virtual event event { get; set; } private int banduserid; public int banduserid { { return banduserid; } set { banduserid = value; } } public virtual band band { get; set; } private datetime startdatetime; public datetime startdatetime { { return startdatetime; } set { startdatetime = value; } } private datetime enddatetime; public datetime enddatetime { { return enddatetime; } set { enddatetime = value; } } #endregion banduserid inherited user base class.
i got working in end overriding onmodelcreating method in datacontext class.
protected override void onmodelcreating(modelbuilder modelbuilder) { modelbuilder.configurations.add(new albumconfiguration()); modelbuilder.configurations.add(new bandconfiguration()); modelbuilder.configurations.add(new cityconfiguration()); modelbuilder.configurations.add(new commentconfiguration()); modelbuilder.configurations.add(new countryconfiguration()); modelbuilder.configurations.add(new countyconfiguration()); modelbuilder.configurations.add(new eventbandsconfiguration()); modelbuilder.configurations.add(new eventfansconfiguration()); modelbuilder.configurations.add(new eventconfiguration()); modelbuilder.configurations.add(new fanconfiguration()); modelbuilder.configurations.add(new genreconfiguration()); modelbuilder.configurations.add(new postconfiguration()); modelbuilder.configurations.add(new recordlabelconfiguration()); modelbuilder.configurations.add(new reviewconfiguration()); modelbuilder.configurations.add(new trackconfiguration()); modelbuilder.configurations.add(new userconfiguration()); modelbuilder.configurations.add(new venueconfiguration()); } the configuration classes contain mappings of primary, composite keys, foreign keys, , other properties.
event configuration:
public class eventconfiguration : entitytypeconfiguration<event> { public eventconfiguration() { #region primary key this.haskey(x => x.eventid); #endregion #region foreign keys this.hasrequired(x => x.venue) .withmany() .hasforeignkey(x => x.venueid) .willcascadeondelete(false); #endregion #region properties this.property(x => x.description).isrequired().hascolumntype("nvarchar"); this.property(x => x.enddatetime).isrequired(); this.property(x => x.name).isrequired().hascolumntype("nvarchar"); this.property(x => x.startdatetime).isrequired(); #endregion } } event bands configuration:
public class eventbandsconfiguration : entitytypeconfiguration<eventbands> { public eventbandsconfiguration() { #region primary key this.haskey(x => new { x.bandid, x.eventid }); #endregion #region foreign keys this.hasrequired(x => x.band) .withmany() .hasforeignkey(x => x.bandid) .willcascadeondelete(false); this.hasrequired(x => x.event) .withmany() .hasforeignkey(x => x.eventid) .willcascadeondelete(false); #endregion #region properties this.property(x => x.startdatetime).isrequired(); this.property(x => x.enddatetime).isrequired(); #endregion } } user configuration:
public userconfiguration() { #region primary key this.haskey(x => x.userid); #endregion #region foreign keys this.hasrequired(x => x.city) .withmany() .hasforeignkey(x => x.cityid) .willcascadeondelete(false); #endregion #region properties this.property(x => x.username); this.property(x => x.loweredusername); this.property(x => x.applicationname); this.property(x => x.email); this.property(x => x.comment); this.property(x => x.password); this.property(x => x.passwordquestion); this.property(x => x.passwordanswer); this.property(x => x.isapproved); this.property(x => x.lastactivitydate); this.property(x => x.lastlogindate); this.property(x => x.lastpasswordchangeddate); this.property(x => x.creationdate); this.property(x => x.isonline); this.property(x => x.islockedout); this.property(x => x.lastlockedoutdate); this.property(x => x.failedpasswordattemptcount); this.property(x => x.failedpasswordattemptwindowstart); this.property(x => x.failedpasswordanswerattemptcount); this.property(x => x.failedpasswordanswerattemptwindowstart); this.property(x => x.mobilealias); this.property(x => x.isanonymous); this.property(x => x.description); this.property(x => x.website); #endregion #region inheritance mapping this.map<fan>(x => x.requires("usertype").hasvalue("fan")) .map<band>(x => x.requires("usertype").hasvalue("band")) .map<venue>(x => x.requires("usertype").hasvalue("venue")); #endregion } } band configuration
public class bandconfiguration : entitytypeconfiguration<band> { public bandconfiguration() { #region foreign keys this.hasrequired(x => x.genre) .withmany() .hasforeignkey(x => x.genreid) .willcascadeondelete(false); #endregion } }
Comments
Post a Comment