如何在实体框架中的自动代码第一迁移中排除一个表?

发布于 2025-02-11 01:27:26 字数 78 浏览 3 评论 0 原文

我正在使用启用自动迁移的代码中的实体框架。现在,我有一个实体,其表应该由EF管理(迁移)。有没有一种方法可以禁用一个特定实体(即表)自动迁移?

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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

嗳卜坏 2025-02-18 01:27:26

现在可以在EF Core 5.0中使用 dubl frommigrations()方法,但奇怪的是,您必须调用 totable()方法,然后使用 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());
    }
}

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.

https://devblogs.microsoft.com/dotnet/announcing-entity-framework-core-efcore-5-0-rc1/#exclude-tables-from-migrations

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());
    }
}
╰沐子 2025-02-18 01:27:26

在EFCORE 5.0中对我有用的另一个选择是使用 setistableExcludedFrommigrations


protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<MyEntity>().Metadata.SetIsTableExcludedFromMigrations(true);
}

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);
}
梦魇绽荼蘼 2025-02-18 01:27:26

我的临时解决方案,仅适用于开发环境。
我有一个单独的脚本运行迁移,并且程序运行不会检查它们。因此,在意外的情况下,我可以调用忽略&lt; contactView&gt;()并使用此行运行迁移。完成后,我删除了这条线!

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // comment out this code after migrations are done
        modelBuilder.Ignore<ContactView>();
    }

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>();
    }
苄①跕圉湢 2025-02-18 01:27:26

通过使用另一个 dbContext 访问所讨论的表。迁移与一个 dbContext (请参阅可以自动迁移一个dbcontext,而不是同一项目中的另一个迁移?)。

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?).

策马西风 2025-02-18 01:27:26

不确定这是否是OP的确切情况,但是我有一个我不希望生成迁移的表。我通过使用 toview 而不是 totable 在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)");
    });
}

对我来说有点骇人听闻,但也许不是 - 因为毕竟,我只是我只是只是,我只是只是尝试“查看”表,不要写入...

[使用.NET CORE EF 3.1.3]

***更新06/17/23:如果您与EF3一起工作,上述解决方案仍然可行但是,如果您使用的是EF5+,则应使用新的 uble dubludeFrommigrations()函数: https://devblogs.microsoft.com/dotnet/announcing-ennouncing-ennouncing-entity-frame-frame-core-core-core-5-5-0-rc1/ #排除列表

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

诠释孤独 2025-02-18 01:27:26

您需要在该类/实体上使用 [未图表] 注释。

You want to use the [NotMapped] annotation on that class/entity.

滥情空心 2025-02-18 01:27:26

问题提到(一个表),但我在视图模型上遇到了这个问题。当您在SQL数据库中正确使用SQL视图及其项目中的模型时,也许您不需要在此视图的项目COZ中添加迁移。解决方案是忽略数据ApaSecontext中的写入dbset,而仅通过fluentapi config或componentmodel.dataannotations进行配置

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文