c# - Entity Framework, Fluent API mapping, should it be done somewhere other than OnModelCreating? -
the question:
my question if i'm going correctly, or whether there's tidier/more-robust way of mapping entities fluent api? mainly, should doing mapping within overidden onmodelcreating method? secondly, there reason not way? , finally, if datamodel working, , i'm able access database, why entity framework powerfulness tools not able generate read-only .edmx file?
so have working code-first info model of ~70 interconnected entities. reverse engineered existing database had been using , entity-framework designer relate objects , map them sql db.
i modeled approach on tutorials i've done, , i'm using fluent api mapping, not info annotations.
so have entity models defined pocos, each in own source file. have class inheriting dbcontext, we'll phone call mydb. within mydb have ~70 dbset properties
public virtual dbset<someentity> someentities{ get; set; } public virtual dbset<otherentity> otherentities{ get; set; } //...you idea, there few dozen of these below public virtual dbset<lastentity> lastentities{ get; set; } //well tedious following dbsets provide access entities, have ~6000 lines of fluent api mappings within protected override void onmodelcreating(dbmodelbuilder mb) , how learned in examples, , in smaller test projects i've done.
protected override void onmodelcreating(dbmodelbuilder mb) { mb.entity<someentity>() .totable("someentities") .haskey(se => se.id); mb.entity<someentity>() .property(se => se.id) .hascolumnname("id") .hascolumntype("bigint") .isrequired() .hasdatabasegeneratedoption(databasegeneratedoption.none); mb.entity<someentity>() .property(se => se.contents) .hascolumnname("content") .hascolumntype("varchar") .ismaxlength() .isunicode(true) .isrequired() .hasdatabasegeneratedoption(databasegeneratedoption.none); mb.entity<someentity>() .hasmany(se => se.otherentities) .withrequired(oe => oe.someentity) .hasforeignkey(oe => oe.someentity_id); //and on, there ~70 of these go on //next 6000 lines } obviously entities aren't simple, , there many unconventional in property naming, table naming, table types, , on; that's why i've been quite verbose fluent api.
i think in terms of performance there's not markable difference whether configuration separate entitytypeconfiguration classes or in onmodelcreating only.
but computer languages not invented help computers, us, binary dyslectic types. therefore, prefer entitytypeconfigurations, because helps specific mapping quicker scrolling through 6000 lines of code. developers spend lot of time referring or others wrote before.
i think issue ef powerfulness tools not generating read-only .edmx file should different question. requires more details on specific problem.
c# asp.net database entity-framework ef-code-first
No comments:
Post a Comment