Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SqlException: Invalid object name c#

I am trying to connect to localDB in my .net core 2.0 web app. I have created a local db using SQL express.

My appsettings.json looks like this

{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
  "Default": "Warning"
}
},

"ConnectionStrings": {
"DefaultConnection": "Data Source=(LocalDb)\\MSSQLLocalDB;Initial Catalog=Shopping;Integrated Security=True;Connect Timeout=60;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
}

}

My Context file looks like this

    public class ProductContext : DbContext
{
    public ProductContext(DbContextOptions<ProductContext> options) 
        :base(options)
    {

    }

    public DbSet<Product> Products { get; set; }
}

My startup file passes context like this

services.AddDbContext<ProductContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

When I am trying to get my products from the Database

        try
        {
            var d = await _context.Products.ToListAsync();
            return View(d);

        }
        catch (Exception e)
        {

        }

I get this exception thrown

"Invalid object name 'Products'."

I am also adding my database image here

enter image description here

I am assuming it has something to do with the database connection?

How do I give path to the file?

like image 870
mohsinali1317 Avatar asked Jan 11 '18 07:01

mohsinali1317


People also ask

What does invalid object name mean in SQL?

This typically means 1 of 2 things... you've referenced an object (table, trigger, stored procedure,etc) that doesn't actually exist (i.e., you executed a query to update a table, and that table doesn't exist). Or, the table exists, but you didn't reference it correctly...

What is System data SqlClient SqlException?

SqlClient. SqlException (0x80131904): A network-related or instance-specific error occurred while extablishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.


Video Answer


2 Answers

Looks like you have no table Products in the Database.

  1. It may be necessary to specify a different table name using the table mapping attribute ([Table("Product")]) on Model or to start migration / initialization on the database, if this has not already happened.
  2. If there was a problem with the connection - the error would be exactly this, but it looks like you are getting an error ("Invalid object name 'Products'.") directly from the sql server
like image 194
Anton Gorbunov Avatar answered Nov 03 '22 00:11

Anton Gorbunov


You can use fluent API in OnModelCreating method of ProductContext to map database table with your model. This way you don't need to add attributes in you models.

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Product>()
            .ToTable("Product");
    }
like image 21
Yared Avatar answered Nov 03 '22 00:11

Yared