I am trying to create a database-first approach ASP.NET Core 6 MVC web app.
I decided to use Microsoft's AdventureWorks
sample database for this.
In short, I am trying to get some information from a table called Production.Product
.
Here is the code:
Product
Class:
public class Product
{
public int ProductID { get; set; }
public string Name { get; set; }
public string ProductNumber { get; set; }
// More properties.
}
Context:
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
public virtual DbSet<Product> Products { get; set; }
}
I add the AppDbContext
in the Program
class as every developer would, nothing special.
For testing purposes I use the HomeController
to get the data.
public class HomeController : Controller
{
private readonly AppDbContext _context;
public HomeController(AppDbContext context)
{
_context = context;
}
// I have the view created.
public IActionResult GetProducts()
{
var model = _context.Products.ToList();
return View(model);
}
}
And when I go to the GetProducts
view, I am greeted with this error:
An unhandled exception occurred while processing the request.
SqlException: Invalid object name 'Products'. Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, bool breakConnection, Action wrapCloseInAction)
I doubled check the connection string. Tried renaming the Product
class to Production_Product
.
In short, I am trying to get some information from a table called Production.Product.And when I go to the GetProducts view, I am greeted with this error:"SqlException: Invalid object name 'Products'". I doubled check the connection string. Tried renaming the Product class to Production_Product.
Well, based on your description, I have successfully reproduce your issue. As you can see below:
Why the error for:
Generally the exception telling us, AppDbContext
property Products
doesn't matched with the AdventureWorks
database schema or table name. Let have a look on the blow screenshot:
As you can see in the database schema table name consturction followed format Production.Product
. Thus, while your entity executing query its searching by DbSet<Product> Products
but Product
obviously doesn't exists or matched as a result we ended with with the error.
How to resolve:
We can solve the error following couple of ways. Here I am completely agreed with @marc_s . Therefore, I am adding, complete implementations.
Way: 1 Using Entity framework Data Annotation:
If you prefer using data annotations of entity framework so you have to modify your Product
class as following:
[Table("Production.Product")]
public class Product
{
public int ProductID { get; set; }
public string Name { get; set; }
public string ProductNumber { get; set; }
}
Note: Main point is that, database schema name should be same as your model or class. You can get more details in official document here.
Way: 2 Using Fluent API Validation:
In this scenario you should modify your code same as blow:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>().ToTable("Production.Product");
}
Full DbContext:
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
public DbSet<Product> Products { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>().ToTable("Production.Product");
}
}
Note: As said earlier, database table name must be same with the AppDbContext
entity name. You can get more details here.
Way: 3 Rename database schema table name:
We even can rename our database table name from Production.Product
to Product
or we can create new table with name Product
. Either way would resolve the issue. Have a look below:
This would also resolve your issue accordingly.
Output:
Note: The takeaways are, your database table name should be matched with your entity model which defined in AppDbContext
. On top of that, you could resolve the issue following either way has been described above.
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.