实体框架数据库连接字符串和类库没有启动项目



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

生成的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复制到启动项目,或者使用显式连接字符串。我相信,在我的用例中没有一个是可用的。

我应该如何解决这个问题?提前谢谢。

好吧,我想明白了。正确的方法是"数据库中的代码优先",它允许使用显式连接字符串。

最新更新