-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Ability to exclude/skip/ignore parts of the model from migrations so that a table is not created (for overlapping bounded contexts) #2725
Comments
Agreed that this would be good to enable. Generalized the title a bit as an attribute may or may not be the best way to do this. An attribute may be good sugar over a more powerful hook to exclude parts of the model. Moving to backlog as we will look at this after the first stable release. |
+1, this to would be extremely useful. |
+1 |
1 similar comment
+1 |
+1 any status on this item? |
1 similar comment
+1 any status on this item? |
No updates. We want to do it, but there are several items higher than it on the backlog. |
Those items listed as higher on the backlog have now been closed. When can we expect inclusion to a sprint? |
The main features we talk about being higher priority are the ones listed here https://docs.microsoft.com/en-us/ef/efcore-and-ef6/features. There is still quite a list of them that have not been implemented. |
I just found a workaround
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Ignore<Category>();
}
public class MigrationContextFactory : IDbContextFactory<MigrationDbContext>
{
public MigrationDbContext Create()
{
var optionsBuilder = new DbContextOptionsBuilder<MigrationDbContext>();
optionsBuilder.UseSqlite("Data Source=blog.db");
return new MigrationDbContext(optionsBuilder.Options);
}
} The migration tool will discover this MigrationContextFactory and use the MigrationDbContext. |
+1, This would make it possible to use ef core in plugin based architectures with each context inheriting from the core context with working relationships between contexts. |
+1 |
1 similar comment
+1 |
I solved it checking from where the entity builder is called, (from "add-migration Xyz" command or the normal dbContext operations) If a migration operation calls the builder, I ignore the entity that I want to ignore just for migration purposes. |
Store the entity types mapped to views in the snapshot Fixes #2725
Store the entity types mapped to views in the snapshot Fixes #2725
Store the entity types mapped to views in the snapshot Fixes #2725
did this ever got added in new versions, +1000000 btw :) |
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 But if I create a migration the whole hierarchy of Of course, I could just remove the line modelBuilder.Entity<Human>().ToTable(nameof(Human), t => t.ExcludeFromMigrations()); and then manually remove the added |
I suggest having an attribute that can be applied to DbSet properties in a DbContext, which indicates whether or not to include them in the migration scaffolding process.
An example use case is when part of an application's database is replicated from another environment. Replicated entities will be included in the same DbContext as custom-developed entities so they can be queried. During development many migrations may be performed. Each time a migration is scaffolded, the replicated entities will be added to the migration, and then must be removed manually.
If an attribute only used for EF commands is too polluting to include in the DbContext, some alternatives might be: an additional config file that can be specified on the command line listing which entities should be excluded. Or perhaps additional command line arguments that allow a migration to be generated for specified entities only instead of scanning and comparing all entities.
Also consider the scenario of inherited models when looking at this feature #5572
The text was updated successfully, but these errors were encountered: