Reference to schema names for tables in the Entity Framework

How do you explicitly tell EF that the table is in a specific schema?

For example, the AdventureWorks database defines the Production.Product table. When using the OnModelCreating method OnModelCreating I use the following code:

 protected override void OnModelCreating(DbModelBuilder modelBuilder) { EntityTypeConfiguration<Product> config = modelBuilder.Entity<Product>(); config.HasKey(p => p.ProductID); config.Property(p => p.Price).HasColumnName("ListPrice"); config.ToTable("Product"); } 

However, when it starts, it says that it is Invalid object name: dbo.Product .

I tried:

 config.ToTable("Production.Product"); //and config.HasEntityName("Production"); 

but both of them do not work either.

+6
source share
2 answers

ToTable has an overloaded version that takes two parameters: the table name and the schema name, so the correct version is:

 config.ToTable("Product", "Production"); 
+12
source

A table schema can also be specified using data annotations using the optional Schema parameter in the Table attribute.

 using System.ComponentModel.DataAnnotations.Schema; [Table("Product", Schema="Production")] public class Product { public int ProductID { get; set; } public decimal Price { get; set; } } 
0
source

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


All Articles