Base solution for your next web application
Open Closed

Turn off migration for existing tables? #3051


User avatar
0
joe704la created

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)
  • User Avatar
    0
    alirizaadiyahsi created

    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.

  • User Avatar
    0
    joe704la created

    @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.

  • User Avatar
    0
    alirizaadiyahsi created

    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.

  • User Avatar
    0
    ismcagdas created
    Support Team

    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.

  • User Avatar
    0
    joe704la created

    Good idea. I will try that. Thank you

  • User Avatar
    0
    ismcagdas created
    Support Team

    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.