[英]Could not load file or assembly C# Console Application with Class Library
[英]'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="data source=LAPTOP-MyDb;initial catalog=EmployeeDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" 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 下载此解决方案,您会明白的,如果有问题,请向我提问。
我曾经在这里遇到过类似的问题,如果您使用的是 n-Tier Design,请确保您已在启动项目中安装了实体框架 package,将连接字符串复制到启动项目的 web.config 或 app.config 中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.