entity-frameworkdeploymentef-code-firstlibrary-project

Entity Framework Code First Library and Database Update Implications


We have recently begun using Entity Framework for accessing all the various databases we touch on a regular basis. We've established a collection of library projects, one for each of these. For many of them, we're accessing established databases that do not change, and using DB first works just great.

For some projects, though, we're developing evolving databases that are having new fields and tables added periodically. Because these libraries are used by multiple projects (at the moment, just two, but eventually many more), running a migration on the production database necessitates a republish of both/all sites that use that particular DB's library. Failure to update the library on any other site of course produces the error that the model backing the context has changed.

How can we effectively manage the deployment/update of the Code-First libraries to all of the sites that use them each time a change to the database is made?


Solution

  • A year later, here's what we came up with and have been using.

    We now include the following line in the Application_Start() method:

    Database.SetInitializer<EFLib.MyHousing.MyHousingMVCContext>(null);
    

    This causes it not to throw a fit if the current database model doesn't exactly match what's in the code. While there is still potential for problems if non-backward-compatible changes are made, this allows for new functionality to be added without the need to re-deploy every site that uses these libraries when the affecting changes are not relevant to that particular site.