Scaffold-DbContext SQL database Views ? ASP NET CORE 6 - entity-framework

I am new to .Net Core and I'd like to know scaffold EF support sql view like it supports sql table in .Net Core 6?
If it supports which command will do? For tables,Scaffold-DbContext and is it same command?
Thank you very much in advance!

Scaffold-DbContext will scaffold both tables and views

Scaffold-DbContext can scaffold both tables and views, Here is the Example:-
Scaffold-DbContext "Server=10.10.10.123;Database=Testdb; user id=sa;
password=Sa1234" Microsoft.EntityFrameworkCore.SqlServer -OutputDir
Models -Context TestDBContext -Tables v_employee

Related

(DB first)I have added one more tables in database after Scaffold. Now trying to add those tables into my model in Entity Framework Core

I have tried it, but it create a master context do not add any new tables into model.
Scaffold-DbContext "Server=localhost;Database=master;Trusted_Connection=True;"
Microsoft.EntityFrameworkCore.SqlServer
-OutputDir Models -Tables OrderDetails -f
Please help.
In the scaffold command you show, you're scaffolding the Master database. That's probably not your application database, or at least it shouldn't be. Master is a system database, and your application should have its own database.
You could add the new tables manually as EF core model classes.

How to generate Entity-context only for one schema when multiple schema available in one database in Postgres sql with .Net Core?

I am using the Postgres database for my project and have created 3 schemas named mydb_dev, mydb_stage, and mydb_prod in database 'temp_db'.
I am creating .Net core web application with the database first approach with entity framework.
Now, whenever I am trying to generate an entity model from the database it generates for all 3 databases. And I want to generate only one database that is mydb_dev.
I am using the following command to generate through the console.
Scaffold-DbContext "Host=localhost;Database=temp_db;Username=myuser;Password=mypassword" Npgsql.EntityFrameworkCore.PostgreSQL -o Models
Is there any way to generate an entity model only for one schema?
If you check the documentation: https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell#scaffold-dbcontext
Scaffold-DbContext has a -Schemas <String[]> parameter.
The schemas of tables to generate entity types for. If this parameter
is omitted, all schemas are included.
So your command should look like this:
Scaffold-DbContext "Host=localhost;Database=temp_db;Username=myuser;Password=mypassword" Npgsql.EntityFrameworkCore.PostgreSQL -o Models -Schemas "mydb_dev"

Update database from models using entity framework in multi tenant .net core

I am using .net core entity framework.
I have multi tenant dbs. so I have kept one root tenant db as base. and I want to replicate those schema changes to all other dbs using entity framework. I am generating my models using following command.
Scaffold-DbContext "Data Source=(local);Initial Catalog=sampleTenantDb;Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Tenants -Force
So when creating new tenant, I simply use
context.Database.EnsureCreated();
But when I add new table in code, I want to apply it to all tenants. So how do I do it?
I tried following but, it doesn't work (not adding remanining tables)
myDbContext.Database.Migrate();
If your DbContext is created with Scaffold-DbContext, it won't have any Migrations. That's for a database-first workflow, in which you would apply the DDL changes to the tenant databases with a script, perhaps created with SQL Server Data Tools.
You have need to create multiple dbcontext object and and pass connection string in that way you can update multiple db

How to update existing model class generated by Scaffold-DbContext

Working with ASP.NET CORE EF, I have generated model classes from existing database with following command:
Scaffold-DbContext "Server=myserver\mydb;Database=mydb;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
Now my database is changed, for example I added a new column in a table AppUser. I don't want to use migrations to keep sync models with database.
I already made changes in database and now I want to update my existing model classes from current database state. Any suggestion how to do this?
Should I delete my existing model classes and regenerate by using the same command Scaffold-DbContext or do we have any other way make existing model classes to reflect new changes.
Are you looking for something like this?
As far as I know, to update the model you must execute the same command to overwrite the changes but with another additional flag.
I remember using the -f (force) option to overwrite the changes:
Scaffold-DbContext "Server=myserver\mydb;Database=mydb;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -f
Although it is also possible to indicate which entity you want to update (table):
Scaffold-DbContext "Server=myserver\mydb;Database=mydb;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -t <table> -f
Scaffold-DbContext can be used with option -Context to expand the current DbContext file, instead of creating a new one.
Example:
Scaffold-DbContext "Server=server;Database=mydb;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -f -Context MyDbContext
The generated code for the MyDbContext will be placed on a partial class file.

Ambiguous column name error when Scaffold-DbContext EF Core

I follow the link to generate EF Core Db context into my project with Northwind database.
I run bellow command for that
Scaffold-DbContext "database=NorthWind;server=localhost;user=sa;password=$pass$4;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir DbContext
But its through an error
Ambiguous column name 'name'.
Can any one tell me whats going wrong?