How to specify the name of the table to be used by DbContext

This is the next question from my previous question . I got the impression that if there is more than one table in the database, the variable name DbSet will be used to identify the table.

However, in a related question, it was clear that DbContext selected the Products table instead of the Portfolio table, even if the variable name was Portfolio . I can still change the name of the variable to everything, and I still get the data.

So my question is, how are tables mapped to a DbContext ? I need to add some more tables to the project and don’t know how to do this using a single DbContext object (or should I use a separate DbContext for individual tables in the same database)?

+6
source share
2 answers

You can do the following in essence:

 [Table("Portfolio")] public class Product { /* ... */ } 

By convention, a table has a name after the plural name of entities.
Thus, by default, DbSet<Product> will be stored / viewed in a table named Products .

+10
source

Take a look at System.ComponentModel.DataAnnotations.Schema.TableAttribute .

In particular:

 [Table("Portfolio")] class Product { public string Foo{get;set;} } 
+3
source

Source: https://habr.com/ru/post/976825/


All Articles