none
Model updated and impacts on database RRS feed

  • Question

  • Hi,

    Currently I develop a website using entity framework code first as ORM. So each time, I modify any class from the model, EF recreates the database and populate this one because I use a class with DropCreateDatabaseIfModelChanges<DbContext>.

    So it's ok in my development environment. But now I begin to transfer the code to the preproduction environment.

    I would like to find a properly solution to permit to not lose all data each time the model evolves.

    I mean by "model evolves", any class from model changes.

    I would like to know if it exists any method permits to do that.

    I imagine two possibilities:

    1\ Just modify the database model, recreate the hash and modify the class model (not friendly and how to recreate the hash in edm table).

    2\a method/process exists permitting to modify the database model regarding the modifications from the class model.

    I find two links:

    http://www.efcodefirst.net/?p=117

    This one talks about severals methods to solve the issue explained. But I think no one can applied.

    • DropCreateDatabaseIfModelChanges
    • DropCreateDatabaseAlways
    • CreateDatabaseIfNotExists
    • CreateDatabaseAlways
    • CreateDatabaseIfModelChanges

    A second link http://stackoverflow.com/questions/6618587/entity-framework-code-first-how-to-manually-update-the-database

    The solutions are:

    • Removing this versioning. It requires removingIncludeMetadataConventionas describedhere.
    • Updating the hash. It would require to reverse engineer the algorithm for hash computation (for example by Red Gate .NET Reflector, JetBrains dotPeek, SharpDevelop ILSpy or Telerik JustDecompile) and computing new hash from compiled model (or using reflection to read internal property fromDbCompiledModel.ModelHashwith already computed hash) which you will store in theEdmMetadatatable.
    • Manually deleting the database and let EF create a new one - you will lose all data
    • Setting initializer toDropCreateDatabaseIfModelChanges- it will automatically delete the database and create a new one if you change the model - you will lose all data

    What is your opinion?

    Did you proceed to a backup of data of the databse before to deploy a new version the first time and after to launch the first time (database recreated by entityframeworkcodefirst), you reload the data?

    Best regards,

    Alexandre


    Best regards, Alexandre http://alexandrebarault.wordpress.com
    Thursday, December 1, 2011 10:18 PM

Answers

All replies