Search code examples
referenceconnection-stringmodelsentities

How to use a model from referenced dll in mvc3 project


I have a class library that has it's own model and connectionstring to the database. Standalone, this works all fine and it's no problem to retrieve data from the database. The idea of this library is to create a dll and reference that in another application.

In my case I have an MVC3 web application with it's own model and connectionstring. I referenced the class library, added a second connectionstring and the whole application builds. But when I am running the appliction and actually call a method on the referenced library, I get an EntitySqlException saying "'MyTable' could not be resolved in the current scope or context. Make sure that all referenced variables are in scope, that required schemas are loaded, and that namespaces are referenced correctly. Near escaped identifier, line 1, column 1."

I made sure that the model files of the referenced library are also copied to the bin directory of the mvc project and my web.config looks like this:

<configSections>
<section name="DefaultArchitecture" type="DefaultArchitectureConfiguration"/>
</configSections> 

<DefaultArchitecture defaultContainerName="defaultContainer" connectionStringName="defaultContainer"/>

<connectionStrings>
<add name="defaultContainer" connectionString="metadata=res://*/MvcModel.csdl|res://*/MvcModel.ssdl|res://*/MvcModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=cloudaddress;Initial Catalog=main catalog;User ID=user;Password=pass;Encrypt=true;Trusted_Connection=false;trustServerCertificate=true&quot;" providerName="System.Data.EntityClient" />
<add name="secondContainer" 
     connectionString="metadata=~\bin\libmodel.csdl|
                                ~\bin\libmodel.ssdl|
                                ~\bin\libmodel.msl;
                       provider=System.Data.SqlClient;
                       provider connection string=&quot;
                       data source=cloudaddress;
                       initial catalog=othercatalog;
                       persist security info=True;
                       user id=user;
                       password=pass;
                       multipleactiveresultsets=True;
                       App=EntityFramework&quot;" 
      providerName="System.Data.EntityClient" />

If I change the defaultcontainername to the secondcontainer...it works fine for the referenced library but of course no longer for the defaultcontainer.

Question is...how can I use both models in my mvc project by having a referenced class library with it's own model (and own database) without having the problem that it cannot resolve one of the entities.

Hope my question is clear. If not, please let me know so I can provide more info.


Solution

  • Eventually it turned out to be a context problem. I was still using the wrong context when calling the class library. I fixed it by implementing an idisposable and switching to the correct context on creationg of the library's service class en switching back on disposal.