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 に答える