Entity Framework throws Invalid object name

764 views Asked by At

I have a DB in SQL Server with several tables.

I have created a Class Library project in VS2013. Created a DBContext, added the database as a ADO.NET file and created a repository for running the queries.

I have created a Web API2 empty project with a controller for creating a REST Api.

This controller is calling the Library repository for running the queries, but it throws an exception:

Invalid object name 'dbo.TLog'

The extrange thing is that this table exists in my database. The query created by the repository is

SELECT [Extent1].[idLog] AS [idLog], 
[Extent1].[description] AS [description], 
[Extent1].[insertDate] AS [insertDate]
FROM [dbo].[TLog] AS [Extent1]

and if I run it in SQL Server it works, but in my web project it doesn't.

The controller looks like this:

[Route("api/log")]
public HttpResponseMessage Get()
{
    var logs = MyRepository.GetAllLogs();
    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, logs);
    return response;
}

The repository query is

var query = from log in dataContext.Log select log;

My connection string only exists in the Library, which connects with the DB, not in the web.config of the web project, and is:

<connectionStrings>
     <add name="MyEntities" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=BBBBBBBB;initial catalog=AAAAAAAA;user id=yyyyyy;password=xxxxxxxxx;persist security info=True;application name=EntityFramework;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>
1

There are 1 answers

0
Kim Johnson On BEST ANSWER

Add the connection string to your web.config. The class library's app.config is not used at runtime.