Reputation: 10323
I'm using the Entity Framework in the Code First mode with automatic migrations enabled. Now, I have one entity whose table should not be managed (migrated) by the EF. Is there a way of disabling automatic migrations for one specific entity (i.e. table)?
Upvotes: 49
Views: 45314
Reputation: 736
The question mentions (one table) but I had this problem with the view model. when you properly an SQL view in an SQL database and a model of it in your project, So maybe you do not need to add migration in your project coz of this view. The solution is to ignore write DbSet in your DataBaseContext and only configure it by fluentApi config or ComponentModel.DataAnnotations
Upvotes: -1
Reputation: 15038
Not sure if this is the OP's exact scenario, but I had a table that I did not want a migration generated for. I accomplished this by using ToView
instead of ToTable
within the DbContext:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<MyTable>(entity => {
// Migration will not be generated for this table
entity.ToView("MyTable", "dbo");
entity.Property(e => e.FooBar).HasColumnType("DECIMAL(19,9)");
});
}
It feels a bit hacky to me, but maybe it's not -- because, after all, I'm just trying to "view" the table, not write to it...
[Tested with .NET Core EF 3.1.3]
*** update 06/17/23: the above solution is still viable if you are stuck working with EF3, but if you are using EF5+ you should use the new ExcludeFromMigrations()
function: https://devblogs.microsoft.com/dotnet/announcing-entity-framework-core-efcore-5-0-rc1/#exclude-tables-from-migrations
Upvotes: 5
Reputation: 1608
Another option that worked for me in EFCore 5.0 is to use SetIsTableExcludedFromMigrations:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEntity>().Metadata.SetIsTableExcludedFromMigrations(true);
}
Upvotes: 49
Reputation: 22833
This is now possible in EF Core 5.0 using the ExcludeFromMigrations()
method, but strangely enough you have to call the ToTable()
method and then use the TableBuilder
.
public class ReportingContext : DbContext
{
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>().ToTable(nameof(Users), t => t.ExcludeFromMigrations());
}
}
Upvotes: 72
Reputation: 7933
My TEMPORARY solution, only for dev environments.
I have a separate script that runs migration and program run does not check them. So in unexpected case I was possible to invoke Ignore<ContactView>()
and run migrations with this line. When it was completed, I removed this line!
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// comment out this code after migrations are done
modelBuilder.Ignore<ContactView>();
}
Upvotes: 14
Reputation: 10323
It is possible by using another DbContext
to access the table in question. Migrations are bound to one DbContext
(see Is it possible to have automatic migrations for one DbContext and not for another in the same project?).
Upvotes: 6