Entity Framework Core 2.2 : Disable migrations for specific entities
I

2

7

I'm trying to build an aspnetcore application over an existing system where the database is already created, and I'm going to add some tables over it.

I've reverse engineered the database to add the existing tables as entities to my application, and I've written my own entities that will be added later. Finally, all the entities are added to a single DbContext.

My requirement comes as follows:

  • I want to enable code first migrations for the newly added entities
  • I don't want the migrations to occur for the already existing tables/entities

Note: I prefer not to touch the generated migration code.

Is there a proper way to handle this scenario?

Intrepid answered 7/2, 2019 at 9:2 Comment(2)
Have you tried this properly : learn.microsoft.com/en-us/ef/core/modeling/included-typesActivator
I'll not be able to read/write to the existing modelsIntrepid
J
7

Is there a proper way to handle this scenario?

Editing the Migration Code is a proper way to handle this scenario.

Alternatively, you could create one DbContext with Migrations containing only the Entities mapped to Tables you want to manage using Migrations. Then create another DbContext with all the Entities which is used to read and write to the database.

Note here that your Migration Context may lack Navigation Properties, containing only the corresponding Foreign Key Properties if you don't want to add real Foreign Keys in the database refering to tables not controlled by your Migrations.

Joan answered 7/2, 2019 at 14:22 Comment(4)
What I've understood is that I have to create a DbContext which corresponds only to applying migrations and should not be used for querying the database. Am I right?Intrepid
Referencing someone else's table with a database Foreign Key will prevent DELETEs and TRUNCATE TABLE, so you may decide that that's too invasive.Joan
What about model builder ignore<> methodBreebreech
ModelBuilder.Ignore will remove the entity from the model entirely. The ask is just to exclude it from Migrations.Joan
J
7

You can ignore the entity if the entity builder is called after Add-Migration operation by examining the command line arguments;

public void Configure(EntityTypeBuilder<YourEntity> builder)
{
    //all properties to be mapped...

    //ignored when adding a new migration
    builder.HasOne(p => p.EntityDetail)
        .WithOne()
        .HasForeignKey<Entity>(x => x.Id)
        .IsRequired(false);

    if (MigrationHelper.IsMigrationOperationExecuting())
        builder.Ignore(x => x.EntityDetail);
}

public static class MigrationHelper
{
    public static bool IsMigrationOperationExecuting()
    {
        var commandLineArguments = Environment.GetCommandLineArgs();
        string[] orderedMigrationArguments = { "migrations", "add" };

        for (var i = 0; i <= commandLineArguments.Length - orderedMigrationArguments.Length; i++)
        {
            if (commandLineArguments.Skip(i).Take(orderedMigrationArguments.Length).SequenceEqual(orderedMigrationArguments))
                return true;
        }

        return false;
    }
}
Jacobs answered 9/6, 2020 at 11:8 Comment(1)
Thank you. I've been trying to solve this on & off for quite some time, and this solution was simple and effective.Minx

© 2022 - 2024 — McMap. All rights reserved.