简体   繁体   中英

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

I am trying to get a list of books from database in my class library project, there is a windows service which is using it. I am using entity framework to query the db, but not able to get the list. Any help would be appreciated. when I added the EF, it has automatically created an App.config inside the class library but looks like the code is not reading from it, neither from the main service project. No idea what to do here? here is my error and code:

No connection string named 'EmployeeDBEntities' could be found in the application config file.

   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)

My class library 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>

My Main service 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>

Nothing fancy just basic configurations, and here is the main class call from class library:

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

I have figured out how to solve. Please download this solution from GitHub and you'll understand, shoot me a question if any.

https://github.com/nilubabai/SelfHostingService

I once ran into a similar issue here, if you're using n-Tier Design then make sure you have installed entity framework package on the startup project, copy the connection string into the web.config or app.config of the startup project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM