如何在 mvc3 项目中使用引用的 dll 中的模型
我有一个类库,它有自己的模型和数据库的连接字符串。独立运行时,一切正常,从数据库检索数据也没有问题。这个库的想法是创建一个 dll 并在另一个应用程序中引用它。
就我而言,我有一个 MVC3 Web 应用程序,它有自己的模型和连接字符串。我引用了类库,添加了第二个连接字符串,然后构建了整个应用程序。但是,当我运行应用程序并实际调用引用库上的方法时,我收到一个 EntitySqlException 异常,提示“无法在当前范围或上下文中解析‘MyTable’。请确保所有引用的变量都在所需架构的范围内已加载,并且命名空间已正确引用,接近转义标识符,第 1 行,第 1 列。”
我确保引用库的模型文件也复制到 mvc 项目的 bin 目录中,我的 web.config 如下所示:
<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="Data Source=cloudaddress;Initial Catalog=main catalog;User ID=user;Password=pass;Encrypt=true;Trusted_Connection=false;trustServerCertificate=true"" 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="
data source=cloudaddress;
initial catalog=othercatalog;
persist security info=True;
user id=user;
password=pass;
multipleactiveresultsets=True;
App=EntityFramework""
providerName="System.Data.EntityClient" />
如果我将默认容器名称更改为第二个容器...它对于引用的库工作正常,但当然不再适用于默认容器。
问题是...如何通过引用的类库及其自己的模型(和自己的数据库)在我的 mvc 项目中使用这两个模型,而不会出现无法解析其中一个实体的问题。
希望我的问题很清楚。如果没有,请告诉我,以便我提供更多信息。
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="Data Source=cloudaddress;Initial Catalog=main catalog;User ID=user;Password=pass;Encrypt=true;Trusted_Connection=false;trustServerCertificate=true"" 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="
data source=cloudaddress;
initial catalog=othercatalog;
persist security info=True;
user id=user;
password=pass;
multipleactiveresultsets=True;
App=EntityFramework""
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最终发现这是一个上下文问题。调用类库时我仍然使用了错误的上下文。我通过实现一个 idisposable 并在创建库的服务类时切换到正确的上下文并切换回处置来修复它。
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.