簡體   English   中英

'EmployeeDBEntities' 可以在 C# Class 庫項目的應用程序配置文件中找到

[英]'EmployeeDBEntities' could be found in the application config file in C# Class Library project

我正在嘗試從我的 class 圖書館項目中的數據庫中獲取書籍列表,有一個 windows 服務正在使用它。 我正在使用實體框架來查詢數據庫,但無法獲取列表。 任何幫助,將不勝感激。 當我添加 EF 時,它自動在 class 庫中創建了一個 App.config,但看起來代碼沒有從它讀取,也沒有從主服務項目讀取。 不知道在這里做什么? 這是我的錯誤和代碼:

在應用程序配置文件中找不到名為“EmployeeDBEntities”的連接字符串。

   at System.Data.Entity.Internal.LazyInternalConnection.get_ConnectionHasModel()
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
   at System.Data.Entity.Internal.InternalContext.Initialize()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator()
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at service.website2.API.TimeController.GetBooks() in ..classfile.cs:line 40
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_2.<GetExecutor>b__2(Object instance, Object[] methodParameters)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)

我的 class 庫 App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="EmployeeDBEntities" connectionString="metadata=res://*/Model.EmployeeDBModel.csdl|res://*/Model.EmployeeDBModel.ssdl|res://*/Model.EmployeeDBModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=LAPTOP-MyDb;initial catalog=EmployeeDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

我的主要服務 app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <!--<add key="connection" value="Initial Catalog=EmployeeDB;data source=LAPTOP-Nilanjan;Integrated Security=True"/>-->
  </appSettings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
</configuration>

沒有什么花哨的基本配置,這里是 class 庫的主要 class 調用:

public IHttpActionResult GetBooks()
        {
            List<Book> booksList;
            using (EmployeeDBEntities entity = new EmployeeDBEntities())
            {
                booksList = entity.Books.ToList(); //this is where error occurs
            }
            return Ok(booksList);
        }

我已經想出了如何解決。 請從 GitHub 下載此解決方案,您會明白的,如果有問題,請向我提問。

https://github.com/nilubabai/SelfHostingService

我曾經在這里遇到過類似的問題,如果您使用的是 n-Tier Design,請確保您已在啟動項目中安裝了實體框架 package,將連接字符串復制到啟動項目的 web.config 或 app.config 中。

暫無
暫無

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

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