如何在使用 EF 数据迁移时将旧数据保留在数据库中

发布于 2024-12-23 03:43:24 字数 1223 浏览 1 评论 0原文

我认为 EF DataMigrations 的目的是在更改数据库结构的同时保留旧数据。我想的对吗?但是当我通过迁移更新数据库时,所有旧数据都会被删除。

我是否做错了什么或者在这种情况下不可能使用数据迁移?

这是 DbContext:

public class CodeFirstContext : DbContext
   {
    public CodeFirstContext() : base ("ZenRandevuIzle")
    {
    }

    public DbSet<User> Users { get; set; }

    public DbSet<Role> Roles { get; set; } 

    public DbSet<Takvim> Takvims { get; set; }

    public DbSet<Musteri> Musteris { get; set; }

    public DbSet<Randevu> Randevus { get; set; }

    public DbSet<SeansTuru> SeansTurus { get; set; }

    public DbSet<Hizmet> Hizmets { get; set; }

    public DbSet<Islem> Islems { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
    }
}

这是 Global.asax:

  Database.SetInitializer<CodeFirstContext>(new CodeFirstContextInit());

这是迁移配置:

internal sealed class Configuration : DbMigrationsConfiguration<CodeFirstContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }
}

I was thinking that the purpose of EF DataMigrations is to preserve old data while changing the database structure. Do I think right? But when I update the database with migrations all the old data is erased.

Do I make something wrong or maybe it isn't possible to use data migrations in this kind of scenario ?

this is DbContext:

public class CodeFirstContext : DbContext
   {
    public CodeFirstContext() : base ("ZenRandevuIzle")
    {
    }

    public DbSet<User> Users { get; set; }

    public DbSet<Role> Roles { get; set; } 

    public DbSet<Takvim> Takvims { get; set; }

    public DbSet<Musteri> Musteris { get; set; }

    public DbSet<Randevu> Randevus { get; set; }

    public DbSet<SeansTuru> SeansTurus { get; set; }

    public DbSet<Hizmet> Hizmets { get; set; }

    public DbSet<Islem> Islems { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
    }
}

this is Global.asax:

  Database.SetInitializer<CodeFirstContext>(new CodeFirstContextInit());

this is migration Config:

internal sealed class Configuration : DbMigrationsConfiguration<CodeFirstContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }
}

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

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

发布评论

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

评论(2

樱花坊 2024-12-30 03:43:24

CodeFirstContextInit 初始化程序是否会删除数据库并重新创建它(它的基类是什么?)通常在使用迁移时我不会有初始化程序。

Does the CodeFirstContextInit initialiser drop the database and recreate it (what is its base class?) normally I wouldn't have an initializer when using migrations.

风追烟花雨 2024-12-30 03:43:24

要将 EF 4.3 Code First 数据迁移与现有数据库结合使用,您需要创建一个“空”迁移,以便创建迁移历史记录表并建立基线。

本文中概述的步骤应该会有所帮助 http://thedatafarm.com/data-access/using-ef-4-3-code-first-migrations-with-an-existing-database

To use EF 4.3 Code First Data Migrations with an existing database you need to create an "empty" migration so that the Migration-History table is created and a baseline can be established.

The Steps outlined in this post should help http://thedatafarm.com/data-access/using-ef-4-3-code-first-migrations-with-an-existing-database

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