首页 > 解决方案 > 没有启动项目的实体框架数据库连接字符串和类库?

问题描述

我在使用实体框架和类库时遇到了一些麻烦。我们已经有了数据库,所以我选择使用数据库优先的方法来获得类型安全。

生成的 dll 文件应该被另一个应用程序使用,它允许以 dll 文件的形式创建自定义“代理”。

为了进行测试,我使用 PowerShell 来实例化类并调用它们的方法。

现在,您可能已经看到了问题所在。没有启动项目这样的东西。我所拥有的只是要使用的类库。

在运行时,EF 找不到 app.config 文件,也无法使用其中的连接字符串。这当然会产生以下错误:

Exception calling "GetPersonStaff" with "0" argument(s): "No connection string named 'StaffEntities' could be found in the application config file."

然后我尝试显式地给继承 DbContext 的类一个连接字符串。但这导致了以下错误:

Exception calling "GetPersonStaff" with "0" argument(s): "The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development.  This
 will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection st
ring is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and t
hat you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.micros
oft.com/fwlink/?LinkId=394715"

我现在有点难过。我试图用谷歌搜索解决方案,但他们都告诉我更改启动项目,将 app.config/web.config 复制到启动项目,或者使用显式连接字符串。我相信,这些都不适用于我的用例。

我应该如何解决这个问题?先感谢您。

标签: c#entity-framework

解决方案


嗯,我想通了。正确的方法是“数据库中的代码优先”,它允许使用显式连接字符串。


推荐阅读