How to exclude one table from automatic code first migrations in the Entity Framework?

后端 未结 5 1354
我寻月下人不归
我寻月下人不归 2020-12-16 05:32

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

5条回答
  •  再見小時候
    2020-12-16 06:04

    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(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]

提交回复
热议问题