What to do when we need another table and a model for it, in case we have already applied the database first approach? (.NET Core, MVC) - .net-core

Whether the steps are the same as for the first time? Is it good to use the -force command to change the old state?

Using Scaffold-DbContext command or dotnet ef dbcontext scaffold command could help generate code for a DbContext and entity types for an existing database.
If you'd like to generate entity types for specific tables, you can set -Tables <String[]> parameter, like below.

Related

How to apply new changes to current database in code fist EF core?

I have a project that coded with DotNet Core code first.
Now, there is a lot of real data on the database and i have to add some features with many tables and new relations to this project.
How can I apply these tables and their relations to the Database without adding manually?
Is there any way to use Add-Migratiosn and Update-Database to apply new changes via EF core instead of manually applying?
If you have not many changes to your DBContext you can remove it and use Scaffolding to create new DbContext from the existing database schema. You can find more information in the official Microsoft documentation here.
It is better to use only one entity framework database approach either Database-First or Code-First.

.NET Core Database First Update Only One Table

I'm using .NET Core 2.1 with Database first approach. I pulled my tables in with the command
Scaffold-DbContext "My_Connection_String;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir DBContext -Context My_Entities -UseDatabaseNames -Tables My_Tables -force
However, when I re-run this command, it re-creates all the table classes and the context. I then have to add virtual to all the relationships and change the namespace which is annoying.
Lets say I update a database table, how do I change my command so only the table I change is updated in the code?
Also, I want to keep with Database first and not switch to Code first.

Entity Framework Core - Scaffold existing database to one or group of class files

As stated in this tutorial, I can scaffold code-first EF Core from an existing database using Scaffold-DbContext in the Package Manager Console.
It's all good but my problem is that it scaffolds separate class files for each table and another for the context class. I find this a bit messy if I have too many tables.
I can rearrange it all by hand but Is there a way to scaffold everything to one class file? Or maybe having the context class on a separate file and then group the tables to another file per schema.
This behavior is not an option of EF Core scaffolding as of 1.0.0.

MigrateDatabaseToLatestVersion seed() doesn't create tables in database [duplicate]

In my application I enable Code First Migrations with some migrations, Also I use SQL Server Compact for integration test.
When I run my tests, Entity Framework create an empty database and tries to run migration on that empty database and thrown The specified table does not exist.
Based on this report I think usage of Migration in Entity Framework 6 has changed.
I test all Database Initializer with Context.Database.Create(); but in all case tabale's never created.
I don't know that this is EntityFramework's bug or not, but when I made rename the namespace of Migration Configuration class from default (Projectname/Migrations) to any none default name, migration works well.
Context.Database.Create() will not execute migrations! It only creates empty db. To Update database from code to latest version you need to use DbMigrator.Update method:
var migrator = new DbMigrator(new MyMigrationsConfiguration());
migrator.Update();
Alternatively you might use MigrateDatabaseToLatestVersion
Database.SetInitializer(new MigrateDatabaseToLatestVersion<BlogContext, Configuration>());
It is described in details here: http://msdn.microsoft.com/en-us/data/jj591621.aspx#initializer
In case someone still struggles to fix the issue.
The code that follows works for me: add-migration MyFirstMigration
Meanwhile add-migration "MyFirstMigration" with the migration name ramped in quote doesn't work.
There may be previous migration files which the ide may be referring to mostly likely due to caching.
Drop backup and drop target database if it exists, and drop the migration folder.
Now add the migration and you will be good to go.
It does happens when adding model and running add-migration command.
Here is the simplest cause of this issue:
Add a newly added model property into IdentityDbContex class.
Here are the steps:
create model
add property into IdentityDbContex class
run add-migration
update-database

Validate EF Code first model against existing database

Is there a way to check that a DbContext matches the database when the database was not created by EF code first?
I am looking for similar functionality to Database.CompatibleWithModel but there is not metadata.
There is currently no way in EF to do this; however, you may be able to use the DDL script as a starting point for verifying that all the artifacts exist in the database. To get this script, use
string ddlScript = ((IObjectContextAdapter)myContext).ObjectContext.CreateDatabaseScript();
Some tools may be able to use this script to do a schema compare against your database. This will tell you if your model is compatible.
Have you tried using Entity Framework Power Tools.
you can use the tools to Reverse Engineer Code First - Generates POCO
classes, derived DbContext and Code First mapping for an existing
database.
And then maybe you can compare the reversed engineered information with what you already have.

Resources