Answered by:
Razor pages and database with a specific schema

Question
-
User1778916669 posted
Hi, I'm building an application for already existing DB. Within a DB there are several schemas eg:
- dbo
- Production,
- Purchasing,
- ...
I don't have any problems to work with dbo schema, because there is no neccesity to refer to dbo anywhere eg:
public DbSet<Employee> Employee { get; set; } that dbo.Employee table
But what would I have to do to connect with Production.Product table?
Thursday, March 4, 2021 9:32 AM
Answers
-
User-821857111 posted
Use the overload of the ToTable fluent API method that enables you to specify the schema: https://www.learnentityframeworkcore.com/configuration/fluent-api/totable-method
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Product>().ToTable("Product", "Production"); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, March 4, 2021 2:22 PM
All replies
-
User475983607 posted
The DB login you are using must be granted read/write access to the Production schema or role that has access to the schema. See SQL support to learn how schemas work.
Thursday, March 4, 2021 12:17 PM -
User-821857111 posted
Use the overload of the ToTable fluent API method that enables you to specify the schema: https://www.learnentityframeworkcore.com/configuration/fluent-api/totable-method
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Product>().ToTable("Product", "Production"); }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, March 4, 2021 2:22 PM -
User1120430333 posted
You can use EF database first or EF code first for an existing database and let EF build the model for you. Why sweat it? If you have relationships and table schemas correct from a DBA standpoint, then EF will make the conceptual model based on what it sees on the table schemas in the database.
Thursday, March 4, 2021 5:19 PM -
User1778916669 posted
Thanks a lot
Friday, March 12, 2021 8:30 AM