EF 迁移是否适用于数据库优先方法?

发布于 2025-01-05 02:19:06 字数 143 浏览 1 评论 0原文

我们在 EntityFramework 中使用数据库优先方法。 我们有多个客户,当我们部署新产品版本时,我们现在使用 SQL Compare 等工具“手动”应用数据库架构更改。

EF 迁移是否可以帮助自动将更改应用到客户数据库?

We're using Database first approach with EntityFramework.
We've several customers, and when we deploy new product version, we're now applying DB schema changes "manually" with tools like SQL Compare.

Is there a way how EF Migrations could help to apply changes to customers DB automatically?

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

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

发布评论

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

评论(4

过度放纵 2025-01-12 02:19:06

据我所知,EF Migrations是一款针对CodeFirst的产品,不支持Database First操作。

CodeFirst 假设您永远不会手动对数据库进行任何更改。对数据库的所有更改都将通过代码优先迁移。

As far as I know, EF Migrations is a product targeted at CodeFirst and doesn't support Database First operations.

CodeFirst assumes that you will never make any changes manually to the database. All the changes to the database will go through the code first migrations.

又怨 2025-01-12 02:19:06

我认为有!您需要先继续阅读代码。

为此,假设您有 EF Db 首先为您创建的以下 DbContext:

public class MyDbContext : DbContext
{
    public MyDbContext()
        : base("Name=DefaultConnection")
    {

    }

    // DbSets ...
}

将其更改为以下内容以首先开始使用代码及其所有神奇工具(迁移等):

public class MyDbContext : DbContext
{
    public MyDbContext()
        : base("YourDbFileName")
    {

    }

    // DbSets ...
}

它会导致 EF 创建一个新连接在您的 web.config 文件中使用 SQL Express 在本地计算机上使用名称为 YourDbFileName 的字符串,就像最初创建的早期 DefaultConnection Db 一样。

继续您的方式可能需要的只是根据您的服务器和其他选项编辑 YourDbFileName ConStr。

更多信息 此处此处

I think there is! You need to continue your way through the code first.

To do this, Suppose that you have the following DbContext that EF Db first created for you:

public class MyDbContext : DbContext
{
    public MyDbContext()
        : base("Name=DefaultConnection")
    {

    }

    // DbSets ...
}

change that to the following to start using code first and all magic tools of it (migration, etc.):

public class MyDbContext : DbContext
{
    public MyDbContext()
        : base("YourDbFileName")
    {

    }

    // DbSets ...
}

It causes that EF creates a new connection string using SQL Express on your local machine in your web.config file with the name YourDbFileName, something just like the early DefaultConnection Db first created.

All you may need to continue your way, is that edit the YourDbFileName ConStr according to your server and other options.

More info here and here.

貪欢 2025-01-12 02:19:06

从 Entity Framework 4.1 开始,您可以执行 代码优先迁移现有数据库

因此,首先您拥有数据库,创建模型,启用迁移。

最重要的是要记住,您应该在对架构进行任何更改之前运行 Enable-Migrations,因为它应该在您的数据库和代码之间保持同步。

Starting Entity Framework 4.1 you can do Code First Migrations with an existing database.

So first you have the database, create the model, enable migrations.

The most important thing to remember that you should run Enable-Migrations before you do any changes to the schema since it should be in sync between your db and code.

清晰传感 2025-01-12 02:19:06

只需查找您的 DbContext 子对象并查找此方法:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

如果您对此进行评论:

throw new UnintentionalCodeFirstException();

则迁移操作时不会引发异常。正如您可能想象的那样,迁移会查找此部分以了解每个实体以及哪些表的配置。

抱歉,如果我没有提供更多详细信息,如果您希望了解更多信息,我很乐意对其进行编辑并使其变得更好!

Just look for your DbContext child object and look for this method:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

If you comment this:

throw new UnintentionalCodeFirstException();

then the exception would not be thrown on migration operation. As you might imagine, the migration look for this part to know what are the configurations for each entity with what table or tables.

Sorry if I didn't go with more details, if you wish more, I'll be happy to edit this and make it better!

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