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.
Related
When I add a new table in my database which use in my database first approach EF project I get some error and problem in my project and newly created DB wasn't attached in my project.
Step 1: After Create Table in Database then open you Package Manager Console
Step 2: Use the following Command for add your new table model class
Scaffold-DbContext "Server=Server name;Database=Database name;User
Id=User name;Password=password;"
Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force
Step 3: Hit Enter
Note: Here only add -Force command with previous migration command
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.
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"
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
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?