1

I am making my project in MVC4, where i am using my Code first approach. i need to update my model

i have a following property which needs to be update , how can i achieve this

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

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

i want to update Phone propery to

public char Phone { get; set; }

thnx in advance, i have installed migrations to my projet already

My 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" }
            //    );
            //
        }
    }
}
4

1 回答 1

1

EF 代码优先的正常流程是先更新模型(C# 文件):

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

然后,您构建您的项目,然后在包管理器控制台中,您必须Add-Migration使用一些标签进行调用(以便稍后在需要时回滚更改):

Add-Migration Phone

这将在您的解决方案中添加一个201409xxxxxxxx_Phone在目录下命名的文件Migrations

然后您必须将更改放入数据库,这可以使用命令完成(始终在控制台中):

Update-Database

然后,您应该完成:该属性Phone到处都是字符串类型。

于 2014-09-23T16:32:46.977 回答