manually updating a database to a EF model RRS feed

  • Question

  • Hello,


    Working with the Entitiy Framework 4.1 I would like to verify, that a database (SqlServerCe) is still compatible with the model (Code First approach). If this is not the case I am planning to update the sdf – database manually.


    To discuss this a little easier, let’s assume I have a DbSet <Project> in my database which has a Property:

    public string Version { get; set;}


    Step 1:

    First I would like to verify the version of the database to load by querying, which Version it contains.


    Step 2:

    If the Version differs, I would like to manually update the database


    Which would be a good way to do this?


    My first attempt failed, starting with a changed model, where I renamed Project.Version to Project.VersionElementA which off course results in a not compatible Model.


    I now thought I could create a instance of my DbContext (ProjectContext) and then query projectContext.Database.CompatibleWithModel(false). But instead of returning false (since the model changed) I get the exception:


    The model backing the 'ProjectContext' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.


    What is it, I am doing wrong? What needs to be done to follow my planed workflow (step1,step2) ?


    Thanks for your help in advance!



    Monday, September 19, 2011 8:44 AM


All replies