I have a database that has some existing tables that I did a database first creation of my entities and then added them to my ASP.NET Zero project. I want to keep migrations but only for the ASP.NET Zero tables. Is there away to shut off migrations for my entities that are existing tables?
6 Answer(s)
-
0
Hi,
If your existing tables different from ASP.NET Zero tables, no problem. If there are tables that have same name, then ef gives error, and dont run migrations.
If you run update-database command on ASP.NET Zero project, your existing tables wont effect.
Advice: Before migration, create a database backup.
-
0
@alirizaadiyahsi it actually does affect it because I have the tables but I am still creating entities in the ASP.NET ZERO project so the migrations pick up that these should be created in the database.
-
0
It doen't matter. EF track the changes. If you create new entities in aspnet-zero application side, EF create or change only tables that is changed at application side.
You can create manually tables at DB side that are not related to aspnet-zero app.
-
0
Hi @joe704la,
After creating entities for your existing tables, add a migration to your project, then remove it's conent and apply it to your database using update database.
After this step, you can use code first migrations for your new entities.
-
0
Good idea. I will try that. Thank you
-
0
Hi,
By the way, I suggest you to generate creating SQL script for your existing database (with or without data) and keep it in your source control. Because in the approach I suggested, you cannot create whole database only using code first migrations. If you somehow loose your existing database, that script will help you to create your database before applying migrations to it.