model updated with [Required] attributes

My model previously was without any DataNotations, but I recently changed that, applying [Required] for some properties. As this happen, my migration code start to throw an exception, like:

Unable to apply pending changes because automatic migration is disabled. To enable automatic migration, ensure that DbMigrationsConfiguration.AutomaticMigrationsEnabled is set to true.

I assume that some explicit actions of migration have to be done. Please clarify.

EDIT : AutomaticMigrationsEnabled = true is not option for me, I'm interesting how to make it possible with some migration scripts.


Add a Configuration class extending DbMigrationsConfiguration and set the AutomaticMigrationsEnabled to true, a sample class would look like this

namespace yournamespace
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

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


        }
    }

then add the configuration class to the DbMigrator instance as follows

namespace yournamespace
{
    public class YourDataMigrator
    {
        public void MigrateData()
        {
            DbMigrationsConfiguration configuration=new Configuration();
            DbMigrator dbMigrator = new DbMigrator(configuration);

            try
            {
                dbMigrator.Update();
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
    }

}

I think this would solve your problem


You can create migration scripts through the package manager console by executing the following scripts:

PM> Enable-Migrations 

followed by

PM> Add-Migration Initial

followed by

PM>  Update-Database

Originally found here: http://www.snippetsource.net/Snippet/146/enable-automatic-migrations-in-ef-codefirst-c


Not sure if I am quite on the same page here, but I think this is what you are asking. I have AutomaticMigrations set to false:

public Configuration()
{
        AutomaticMigrationsEnabled = false;
}

I have a Post class set like this:

public class Post
{
    public int Id { get; set; }
    public string Title { get; set; }
    [Required]
    public DateTime DateCreated { get; set; }
    public string Content { get; set; }
    public string Tags { get; set; }
    public ICollection<Comment> Comments { get; set; }
}

It is already generated, but then I realized for some reason I want title to be required.

public class Post
{
    public int Id { get; set; }
    [Required]
    public string Title { get; set; }
    [Required]
    public DateTime DateCreated { get; set; }
    public string Content { get; set; }
    public string Tags { get; set; }
    public ICollection<Comment> Comments { get; set; }
}

I make the change, do a quick build, and then from the PM Console I type:

Add-Migration AddPostAnnotation (Name can be whatever you want)

This generates this file:

public partial class AddPostAnnotations : DbMigration
{
    public override void Up()
    {
        AlterColumn("dbo.Posts", "Title", c => c.String(nullable: false));
    }

    public override void Down()
    {
        AlterColumn("dbo.Posts", "Title", c => c.String());
    }
}

Once this is here, I just run Update-Database from the PM console, and the changes are sent over.

链接地址: http://www.djcxy.com/p/90246.html

上一篇: 在不同的TFS分支上工作时丢失EF代码首次迁移?

下一篇: 模型用[必需的]属性更新