2014-09-23 40 views
1

我正在使用我的代码优先方法在MVC4中制作项目。我需要更新我的模型带代码迁移的MVC更新模型

我有以下属性,它需要被更新,我怎么能做到这一点

public class ContactForm 
    { 
     public char Phone { get; set; } 
    } 

    public class ConContext : DbContext 
    { 
     public DbSet<ContactForm> ContactForms { get; set; } 
    } 
} 

我想更新手机属性格式来

public char Phone { get; set; } 

日Thnx提前,我已经安装迁移到我谟已经

我configuration.cs

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

    internal sealed class Configuration : DbMigrationsConfiguration<MyCRM.Models.ConContext> 
    { 
     public Configuration() 
     { 
      AutomaticMigrationsEnabled = false; 
     } 

     protected override void Seed(MyCRM.Models.ConContext 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" } 
      // ); 
      // 
     } 
    } 
} 
+0

哪部分不起作用?代码的更新,控制台上的迁移或部署到数据库? – 2014-09-23 14:17:19

+0

当我在代码中简单更新时,我们必须始终删除我的数据库,这不是那么古德@SamuelCaillerie – 2014-09-23 14:18:59

+0

当您运行“添加迁移”时,这会在您的解决方案中生成一个新的可更新的c#文件。它在你的案例中产生了什么:一列旧列并添加新列?您可以查看http://msdn.microsoft.com/en-us/data/jj591621.aspx#customizing以定制您的迁移。 – 2014-09-23 14:26:05

回答

1

的正常流动与EF代码优先过于先更新模型(C#文件):

public class ContactForm 
{ 
    public string Phone { get; set; } //previously, this was let's say of type int 
} 

然后,你建立你的项目,之后在Package Manager控制台,你必须调用Add-Migration与某些标签(以便在需要时以后回滚的变化):

Add-Migration Phone 

这将添加到您的解决方案名为像这样201409xxxxxxxx_Phone目录Migrations下的文件。

然后,你必须把它用命令(总是在控制台)来完成的更改数据库:

Update-Database 

然后,你应该做的事:物业Phone是字符串类型的无处不在。

+0

绝对好! :) – 2014-09-23 17:19:52