简体   繁体   English

实体框架迁移-自动创建表和更新

[英]Entity Framework Migrations - Auto Create Tables & Update

I maybe not understanding Migrations, but I am updating my opensource application to use them. 我可能不了解迁移,但是我正在更新我的开源应用程序以使用它们。 My goal was 我的目标是

1) When a users runs the application for the first time with a blank database, the application auto creates the tables (And hits my seed method to auto populate required data). 1)当用户首次使用空白数据库运行该应用程序时,该应用程序会自动创建表(并点击我的seed方法以自动填充所需的数据)。

2) If I add any new properties to my Entity/Model classes, they are auto added when the database. 2)如果我将任何新属性添加到我的实体/模型类,则在数据库时会自动添加它们。

Is it possible to do this without using the Nuget commands? 是否可以不使用Nuget命令来执行此操作? As I was hoping this would take away a lot of my installer code for initially creating tables and adding data on first install. 正如我希望的那样,这将减少大量安装程序代码,这些代码最初用于创建表并在首次安装时添加数据。

I have the following set up 我有以下设置

Database.SetInitializer(new MigrateDatabaseToLatestVersion<MVCForumContext, Migrations.Configuration>(AppConstants.MvcForumContext));

And my configuration class 还有我的配置类

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

    protected override void Seed(MVCForumContext context)
    {
        //  This method will be called after migrating to the latest version.

        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data. E.g.
        //
        //    context.People.AddOrUpdate(
        //      p => p.FullName,
        //      new Person { FullName = "Andrew Peters" },
        //      new Person { FullName = "Brice Lambson" },
        //      new Person { FullName = "Rowan Miller" }
        //    );

        //TODO - ADD THE INITIAL DATA HERE
    }
}

And I have all my model classes mapped properly using the fluent API (ie) 我使用流利的API(即)正确映射了所有模型类

public class BadgeMapping : EntityTypeConfiguration<Badge>
{
    public BadgeMapping()
    {
        HasKey(x => x.Id);

        Property(x => x.Id).IsRequired();
        Property(x => x.Name).IsRequired().HasMaxLength(50);
        Property(x => x.Description).IsOptional();
        Property(x => x.Type).IsRequired().HasMaxLength(50);
        Property(x => x.Image).IsOptional().HasMaxLength(50);
        Property(x => x.DisplayName).IsRequired().HasMaxLength(50);
        Property(x => x.AwardsPoints).IsOptional();

        // Ignores
        Ignore(x => x.Milestone);
    }
}

Again, I may be mis-understanding. 同样,我可能会误会。 But I created a blank database and ran the app pointing at the database. 但是我创建了一个空白数据库,然后运行指向该数据库的应用程序。

It failed to create the tables with all the fields as I suspected? 我怀疑无法创建包含所有字段的表吗? Am I missing something here? 我在这里想念什么吗?

I have been developing against an existing database, and to get it working I found a blog post outlining what you need to do to get it working with an existing database. 我一直在针对现有数据库进行开发,并且要使其正常工作,我发现了一篇博客文章,概述了如何使其与现有数据库一起工作。

Simply run the add-migration initial command in the package manager console window, browse to the Migrations folder, modify the new file that has been added, it will contain a class that inhereits DbMigration, with two methods, Up and Down. 只需在程序包管理器控制台窗口中运行add-migration initial命令,浏览到Migrations文件夹,修改已添加的新文件,它将包含一个使用DbMigration继承此类的类,该类具有两种方法,即Up和Down。 If you remove all of the content of the methods, but leave the methods, then run the Update-Database -Verbose command in the package manager console it will work. 如果删除方法的所有内容,但保留方法,然后在程序包管理器控制台中运行Update-Database -Verbose命令,它将起作用。

So I have cleared the UP() and Down() methods. 因此,我已经清除了UP()和Down()方法。 If I put those back, then I can't run against an existing database. 如果我将它们放回原处,那么我将无法在现有数据库上运行。

Maybe it's just me thinking it's going to be smarter than I thought. 也许只是我认为它会比我想象的要聪明。 Knowing to create tables if needed and auto update missing fields based on the model mappings? 是否知道根据需要创建表并根据模型映射自动更新缺少的字段?

You need to use add-migration every time when you change your model classes. 每次更改模型类时,都需要使用add-migration。 When you run app for the first time after changes new migrations are applied to you database. 更改后首次运行应用程序时,新迁移将应用于您的数据库。 With first migration your database gets the MigrationHistory table and then it is being used for checking which migrations have been applied and which need to be applied. 首次迁移时,您的数据库将获得MigrationHistory表,然后将其用于检查已应用哪些迁移以及需要应用哪些迁移。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM