Search code examples
entity-frameworkef-code-first

EF 6 How to skip existing tables when generating migrations


I am trying to use code-first within my existing project. I want to exclude existing entities from migrations I generate for new entities.

I have all models in separate class library project e.g. Data.Models and I am intending to use one context by creating another class library e.g. Infra.EF (model project is referenced in it).

This is how my DbContext looks like:

    public DbSet<ExistingEntityOne> DataOfEntityOne { get; set; }
    public DbSet<ExistingEntityTwo> DataofEntityTwo { get; set; }
    public DbSet<NewEntity> NewData { get; set; }

Sorry if question isn't clear but I can add more information based on your feedback.

Thanks.


Solution

    1. Remove Migration directory from your project.
    2. Comment new entities in your new DbContext.

      public DbSet<ExistingEntityOne> DataofEntityOne { get; set; }
      public DbSet<ExistingEntityTwo> DataofEntityTwo { get; set; }
      //public DbSet<NewEntity> NewData { get; set; }
      
    3. Run following commands In power console manager:

      Enable-Migrations
      Add-Migration somename -IgnoreChanges
      
    4. uncomment, commented entities.

      public DbSet<ExistingEntityOne> DataofEntityOne { get; set; }
      public DbSet<ExistingEntityTwo> DataofEntityTwo { get; set; }
      public DbSet<NewEntity> NewData { get; set; }
      
    5. Run following command:

      Add-Migration someOtherName
      

    You can find more, for EF Migrations here