简体   繁体   中英

Entity Framework throws Invalid object name

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>

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

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