Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude only base type table from Migrations #30079

Closed
ajcvickers opened this issue Jan 17, 2023 · 2 comments · Fixed by #31594
Closed

Exclude only base type table from Migrations #30079

ajcvickers opened this issue Jan 17, 2023 · 2 comments · Fixed by #31594
Labels
area-migrations breaking-change closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@ajcvickers
Copy link
Member

Originally filed as #2725 (comment) by VeMike

So how would I ignore just a base class, but not a derived class using this approach?

Consider the following model:

[Table(nameof(Human))]
public class Human
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int HumanId { get; set; }
    
    public int Age { get; set; }
}

[Table(nameof(Developer))]
public class Developer : Human
{
    public string MainLanguage { get; set; }
}

The database context for this model is the following

public class FooContext : DbContext
{
    public DbSet<Developer> Developers { get; set; } = null!;

    /// <inheritdoc />
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // This is made to get TPT
        modelBuilder.Entity<Human>().ToTable(nameof(Human));
        modelBuilder.Entity<Developer>().ToTable(nameof(Developer));
        
        modelBuilder.Entity<Human>().ToTable(nameof(Human), t => t.ExcludeFromMigrations());
    }

    /// <inheritdoc />
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);

        optionsBuilder.UseSqlServer("<FooBarConnectionString>");
    }
}

As you can see I only want Developer to be in the migration. Human should be excluded. Why would I want to do this? I am extending an existing model and the base (Human) already exists in the database. I want to add Developer to this existing model.

But if I create a migration the whole hierarchy of Human is excluded (basically all classes of type Human). But I want the Developer to be created.

Of course, I could just remove the line

modelBuilder.Entity<Human>().ToTable(nameof(Human), t => t.ExcludeFromMigrations());

and then manually remove the added Human from the migration. But is there a way to do this automatically?

@AndriySvyryd
Copy link
Member

AndriySvyryd commented Jan 18, 2023

Note to implementor:

Add TPC condition to:

if (entityType.BaseType != null)
{
return entityType.GetRootType().IsTableExcludedFromMigrations();
}

Also check that the dependent is mapped to the same table in:

if (ownership != null
&& ownership.IsUnique)
{
return ownership.PrincipalEntityType.IsTableExcludedFromMigrations();
}

Change to ||:

if (source.IsExcludedFromMigrations
&& target.IsExcludedFromMigrations)

Check IsExcludedFromMigrations in:

r => r.EntityState is EntityState.Added or EntityState.Modified
|| (r.EntityState is EntityState.Deleted && diffContext.FindDrop(r.Table!) == null));

@ajcvickers
Copy link
Member Author

Note from triage: this is a bug, but fix should be documented as a breaking change since TPT cases where all tables were excluded will now only exclude the base table.

@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 31, 2023
@AndriySvyryd AndriySvyryd removed their assignment Aug 31, 2023
@ajcvickers ajcvickers modified the milestones: 8.0.0, 8.0.0-rc2 Sep 6, 2023
@ajcvickers ajcvickers modified the milestones: 8.0.0-rc2, 8.0.0 Nov 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-migrations breaking-change closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants