Saturday, August 9, 2014

how to migrate database using entity framework when c# application runtime

PM> enable-migrations -Force
Checking if the context targets an existing database...
Detected database created with a database initializer. Scaffolded migration '201407300507349_InitialCreate' corresponding to existing database. To use an automatic migration instead, delete the Migrations folder and re-run Enable-Migrations specifying the -EnableAutomaticMigrations parameter.
Code First Migrations enabled for project HO.DIRECTORY.GUIDE.


1.
public sealed class Configuration : DbMigrationsConfiguration<HO.DIRECTORY.GUIDE.DbContextDirectoryGuide>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
            ContextKey = "HO.DIRECTORY.GUIDE.DbContextDirectoryGuide";
        }
2.
/// <summary>
/// програм ажиллаж байх явцад тохиолдсон асуудлуудыг тэмдэглэж үлдээнэ
/// </summary>
public class NotationInfo : NamedMetaData
    {
    }

3.
public DbSet<NotationInfo> NotationInfoes { get; set; }

4.
/// <summary>
/// Өгөгдлийн санд өөрчлөлт хийх үед энэ кодыг ашиглана
/// </summary>
public class DbInitializer : MigrateDatabaseToLatestVersion<DbContextDirectoryGuide, Migrations.Configuration>
    {
    }

5.
Database.SetInitializer(new DbInitializer());
//Database.SetInitializer<DbContextDirectoryGuide>(new DbContextDirectoryGuideInitializer());


PM> Add-Migration NotationInfoes
Scaffolding migration 'NotationInfoes'.
The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration NotationInfoes' again.
PM>



RUN Application then automatically change database without drop or create

No comments: