簡體   English   中英

沒有啟動項目的實體框架數據庫連接字符串和類庫?

[英]Entity Framework database connection string and class library without startup project?

我在使用實體框架和類庫時遇到了一些麻煩。 我們已經有了數據庫,所以我選擇使用數據庫優先的方法來獲得類型安全。

生成的 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 復制到啟動項目,或者使用顯式連接字符串。 我相信,其中沒有一個在我的用例中可用。

我應該如何解決這個問題? 先感謝您。

嗯,我想通了。 正確的方法是“Code First from Database”,它允許使用顯式連接字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM