'EmployeeDBEntities'可以在C#类库项目的应用程序配置文件中找到

问题描述 投票:0回答:1

我正在尝试从我的班级图书馆项目中的数据库中获取书籍列表,有一个正在使用它的Windows服务。我正在使用实体框架查询数据库,但无法获取列表。任何帮助,将不胜感激。当我添加EF时,它已经在类库中自动创建了一个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)

我的班级库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>

没有什么只是基本配置,这是来自类库的主要类调用:

public IHttpActionResult GetBooks()
        {
            List<Book> booksList;
            using (EmployeeDBEntities entity = new EmployeeDBEntities())
            {
                booksList = entity.Books.ToList(); //this is where error occurs
            }
            return Ok(booksList);
        }
c# .net entity-framework connection-string class-library
1个回答
0
投票

我已经知道了如何解决。请从GitHub下载此解决方案,您会明白的,请问我一个问题。

https://github.com/nilubabai/SelfHostingService

© www.soinside.com 2019 - 2024. All rights reserved.