none
Programmatically update database with Entity Framework 6 and code first RRS feed

  • Question

  • Hi,

    when a User first starts it's application a local client sql ce database is created. This is realized by the code first approach. 

    What I want now is to keep the database up-to-date with my code changes which will be released sooner or later. 

    How do I programmatically update the database e.g. with the help of the "MigrateDatabaseToLatestVersion" - Initializer?

    I just need a clarification in how to use the different base classes like "DbMigration" or "DbMigrator" or "Configuration".

    Thank you.

    Monday, December 2, 2013 4:05 PM

Answers

  • Hello,

    >>How do I programmatically update the database e.g. with the help of the "MigrateDatabaseToLatestVersion" - Initializer?

    For this, first we need to enable the automatic migrations using command like below:

    Enable-Migrations –EnableAutomaticMigrations -ContextTypeName CodeFirstModel.S201312.BlogContext

    Then in the mydbcontext add codes like below:

    public BlogContext()
    
            {
    
                Database.SetInitializer<BlogContext>(new MigrateDatabaseToLatestVersion<BlogContext, Configuration>());
    
            }
    

    After this, it will update the database to the newest version if we have do any changes in the codes.

    Note, there is a limit that we could only add the properties or associations, because automatic migration was not applied because it would result in data loss if we reduce properties or associations.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by faa-db Tuesday, December 3, 2013 3:15 PM
    Tuesday, December 3, 2013 6:17 AM
    Moderator

All replies

  • Hello,

    >>How do I programmatically update the database e.g. with the help of the "MigrateDatabaseToLatestVersion" - Initializer?

    For this, first we need to enable the automatic migrations using command like below:

    Enable-Migrations –EnableAutomaticMigrations -ContextTypeName CodeFirstModel.S201312.BlogContext

    Then in the mydbcontext add codes like below:

    public BlogContext()
    
            {
    
                Database.SetInitializer<BlogContext>(new MigrateDatabaseToLatestVersion<BlogContext, Configuration>());
    
            }
    

    After this, it will update the database to the newest version if we have do any changes in the codes.

    Note, there is a limit that we could only add the properties or associations, because automatic migration was not applied because it would result in data loss if we reduce properties or associations.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by faa-db Tuesday, December 3, 2013 3:15 PM
    Tuesday, December 3, 2013 6:17 AM
    Moderator
  • Thank you.

    It was kind of confusing.

    I implemented a ContextFactory because the connectionstring differs from time to time. So I had no default constructor neither in the ContextFactory class nor in the Context-Class itself.

    So when accessing the context for the first time it called the factory-class with the connectionstring which in turn returned a context instance with this connectionstring. 

    But within the using block when accessing the context instance a second time the default constructor is called and the connectionsstring not set. So it always threw an exception. Making the local field holding the connectionstring static helped in that case. 

    But I still don't get why this:

    using(BlogContext context = new BlogContextFactory(conn).Create()){

    context.Add(new Blog{ Name="something"});

    context.SaveChanges();

    }

    will call:

    ctor BlogContextFactory(string conn) in the "using" line and 

    ctor BlogContextFactory() in "context.Add" line??

    Tuesday, December 3, 2013 3:28 PM