asp.net mvc - Problems with DbContext in Class Project -


background:

i have web api project , class library project in same solution share same model classes. both projects share same database , both use dbcontext read/write data.

the web api project set in typical unitofwork pattern , works fine.

the class project bit different. specify connection string in constructor instead of web.config file:

class mycontext : dbcontext {     public mycontext(string connectionstring)         : base()     {         // force me use eager loading         this.configuration.lazyloadingenabled = false;         // init connection string         this.database.connection.connectionstring = connectionstring;         database.setinitializer <cfcalccontext> (null);     }       ... dbsets ...      protected override void onmodelcreating(dbmodelbuilder modelbuilder)     {         ... modelbuilder details ...     } } 

i use web api call function defined in class library. when attempt query database using class library error.

the problem:

in class library project entitycommandcompilationexception inner exception as:

innerexception: system.data.mappingexception hresult=-2146232032 message= (82,10) : error 3034: problem in mapping fragments starting @ lines 82, 90:two entities different keys mapped same row. ensure these 2 mapping fragments not map 2 groups of entities different keys same group of rows.

(82,10) : error 3034: problem in mapping fragments starting @ lines 82, 98:two entities different keys mapped same row. ensure these 2 mapping fragments not map 2 groups of entities different keys same group of rows.

(82,10) : error 3034: problem in mapping fragments starting @ lines 82, 106:two rows different primary keys mapped same entity. ensure these 2 mapping fragments not map 2 groups of entities identical keys 2 overlapping groups of rows.

(82,10) : error 3034: problem in mapping fragments starting @ lines 82, 112:two entities different keys mapped same row. ensure these 2 mapping fragments not map 2 groups of entities different keys 2 overlapping groups of rows.

(82,10) : error 3034: problem in mapping fragments starting @ lines 82, 118:two entities different keys mapped same row. ensure these 2 mapping fragments not map 2 groups of entities different keys 2 overlapping groups of rows.

(90,10) : error 3034: problem in mapping fragments starting @ lines 90, 98:two entities different keys mapped same row. ensure these 2 mapping fragments not map 2 groups of entities different keys same group of rows.

also, when right click derived dbcontext class (in class library) , use entity framework power tools "view entity data model" error: "a constructible type deriving dbcontext not found in selected file." works fine in web api project.

why 2 dbcontexts connect same database , configured same perform differently? must missing configuration step, don't know which.

thanks help!

a lot going on here. firstly, if you're manually setting connection string should setting in call base constructor:

public mycontext(string connectionstring)     : base(connectionstring) 

secondly though, should putting in web.config - if name issue, can give different name like:

public mycontext()     : base("whatever name used in web.config") 

third wrong:

database.setinitializer <cfcalccontext> (null); 

the documentation database.setinitializer states:

gets or sets database initialization strategy. database initialization strategy called when dbcontext instance initialized dbcompiledmodel.

in other words setting null make dbcontext explode tries - suspect that's what's happening here.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -