简体   繁体   中英

SqlException: Invalid object name 'service'

I am working on getting up to speed with ASP.NET MVC for web development. I get the can't find object error,

SqlException: Invalid object name 'service'

that seems to be common. I have verified that the table exists and the database exists and the table name doesn't have spaces before or after it. I have access to the database or I would get another error.

In my service class I use the table classifier to tell the system the actual table name as below.

[Table ("service")]
public class service
{
    [Key]
    public int service_id { get; set; }
    public string service_name { get; set; }
    public string service_logo { get; set; }

    public int externalcontacts_id { get; set; }
    public int classification_id { get; set; }
}

The error happens when I try to access the database to get live data to pass to my view. I have tried more formal LINQ queries but regardless of what I do I get the same error. The current code that generates it is as below.

public async Task<IActionResult> Index()
{
    var serviceList = await (_context.Services).ToListAsync();

    return View(serviceList);
}

I'm using Visual Studio 2019. I can see the database in the Server Explorer and access the data but I can't access it from my app. I am not the DB administrator. Do they need to give privileges to my application? I don't know. I have never worked with SQL Server.

Could be you've got the wrong database, wrong schema, the database is case-sensitive, and you've got the case wrong, or you're connecting with the wrong user. Query the catalog from your application code and see if you can see the table. eg what does this return?

select quotename(schema_name(schema_id)) + '.' + quotename(name) table_name
from sys.tables  t
where t.name like '%service%' collate Latin1_General_CI_AI

为了解决这个问题,我必须将架构名称添加到系统中。

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