fluent interface - Creating multiple contexts in Entity Framework Code First -
i struggling implement multiple database contexts in entity framework code first. have implemented 3 contexts (all entities, entities related conference , entities related user profile) shown on pictures.
fluent api used entity mappings. example code used conferencecontext:
public class conferencecontext : basecontext<conferencecontext> { public dbset<conference> conferences { get; set; } public dbset<importantdate> importantdates { get; set; } public dbset<topic> topics { get; set; } public dbset<venue> venues { get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { base.onmodelcreating(modelbuilder); insertentitymappings(modelbuilder); // inserts entity mappings } private void insertentitymappings(dbmodelbuilder modelbuilder) { modelbuilder.configurations.add(new conferencemappings()); modelbuilder.configurations.add(new importantdatemappings()); modelbuilder.configurations.add(new topicmappings()); modelbuilder.configurations.add(new venuemappings()); modelbuilder.ignore<conferencecomment>(); modelbuilder.ignore<conferencerating>(); modelbuilder.ignore<connectionconferencerecommendation>(); modelbuilder.ignore<userprofile>(); } } public class conferencemappings : entitytypeconfiguration<conference> { public conferencemappings() { // primary key this.haskey(t => t.conferenceid); this.property(t => t.conferenceid).hasdatabasegeneratedoption(databasegeneratedoption.identity); // properties this.property(t => t.title) .isrequired() .hasmaxlength(150); this.property(t => t.summary) .hasmaxlength(1000); this.property(t => t.contactemail) .hasmaxlength(100); this.property(t => t.websiteurl) .isrequired() .hasmaxlength(100); this.property(t => t.logouri) .hasmaxlength(100); // table & column mappings this.totable("conference"); this.property(t => t.conferenceid).hascolumnname("conferenceid"); this.property(t => t.title).hascolumnname("title"); this.property(t => t.summary).hascolumnname("summary"); this.property(t => t.contactemail).hascolumnname("contactemail"); this.property(t => t.websiteurl).hascolumnname("websiteurl"); this.property(t => t.logouri).hascolumnname("logouri"); this.property(t => t.begginingdate).hascolumnname("begginingdate"); this.property(t => t.endingdate).hascolumnname("endingdate"); this.property(t => t.venueid).hascolumnname("venueid"); // relationships this.hasmany(t => t.topics) .withmany(t => t.conferences) .map(m => { m.totable("topicconference"); m.mapleftkey("conference_conferenceid"); m.maprightkey("topic_topicid"); }); this.hasoptional(t => t.venue) .withmany(t => t.conferences) .hasforeignkey(d => d.venueid); } } public class importantdatemappings : entitytypeconfiguration<importantdate> { public importantdatemappings() { // primary key this.haskey(t => t.importantdateid); this.property(t => t.importantdateid).hasdatabasegeneratedoption(databasegeneratedoption.identity); // properties this.property(t => t.eventname) .isrequired() .hasmaxlength(100); // table & column mappings this.totable("importantdate"); this.property(t => t.importantdateid).hascolumnname("importantdateid"); this.property(t => t.date).hascolumnname("date"); this.property(t => t.eventname).hascolumnname("eventname"); // relationships this.hasrequired(t => t.conference) .withmany(t => t.importantdates) .hasforeignkey(t => t.conferenceid); } } public class topicmappings : entitytypeconfiguration<topic> { public topicmappings() { // primary key this.haskey(t => t.topicid); this.property(t => t.topicid).hasdatabasegeneratedoption(databasegeneratedoption.identity); // properties this.property(t => t.name) .isrequired() .hasmaxlength(100); // table & column mappings this.totable("topic"); this.property(t => t.topicid).hascolumnname("topicid"); this.property(t => t.name).hascolumnname("name"); } } public class venuemappings : entitytypeconfiguration<venue> { public venuemappings() { // primary key this.haskey(t => t.venueid); this.property(t => t.venueid).hasdatabasegeneratedoption(databasegeneratedoption.identity); // properties this.property(t => t.address) .hasmaxlength(100); this.property(t => t.city) .hasmaxlength(50); this.property(t => t.state) .hasmaxlength(50); // table & column mappings this.totable("venue"); this.property(t => t.venueid).hascolumnname("venueid"); this.property(t => t.address).hascolumnname("address"); this.property(t => t.city).hascolumnname("city"); this.property(t => t.state).hascolumnname("state"); this.property(t => t.countryid).hascolumnname("countryid"); // relationships this.hasoptional(t => t.country) .withmany(t => t.venues) .hasforeignkey(d => d.countryid); } }
can me create context similar 1 shown on picture below.
Comments
Post a Comment