I’ve been watching, with keen interest, the progression of Entity Framework Code First development over the last year or two while contrasting it with the more traditional Entity Framework model first development and it’s led me to a place of much confusion and internal conflict.
I’ve been using Entity Framework since version 1 and at first found the EDMX designer a source of annoyance and frustration. However as Entity Framework progressed to v4, VS 2010 was released and I became more familiar with the workings of Entity Framework generally I have come to love designing my models in the EDMX designer, using customised T4 templates to generate the C# code models (including many validation attributes) and a context to be used with a generic, re-usable, repository and, importantly for me, using the Entity Framework Database Generation Power Pack to sync a DB project with my model. The whole process requires me only to add/edit/remove from the designer and everything else is quite automated. The DB project also allows me to deploy not only the database, but also test and reference data, to many environments in many different states.
I can see many attractions to code first development, it’s definitely simpler in terms of setup and abstracts away a lot of the database level stuff that, as a web developer, I’m reluctant to get heavily involved in. It also seems that development on code first features is pushing ahead quite rapidly and the new migration features are a big win for control over database deployment. It’s also undoubtedly cool and will continue to blow my mind in inventive ways with each and every release.
However, and here’s the source of my internal conflict, I can’t help but feel that properly set up model first development with a synced DB project still has some advantages and in a lot of ways makes a more compelling case as an ‘enterprisey’ ORM solution. Sure it can be more complex at times but I feel the simplicity could be greatly improved with continued development of things like the Entity Framework Database Generation Power Pack and Visual Studio support. And what if you want other items in the database that aren’t represented in the code first model, like ASP.NET Membership? Or what if you need to create the database from scratch including logins, users, roles, etc? How do you integrate it cleanly with a CI build? Do you lose some control when deploying to multiple environments (e.g. test, production), particularly for reference and test data, and tracking the versions of databases between these for bug testing and fixing? All of those things are handled with a model first approach but seem to be lacking in some way with code first.
For example I’ve set up the ASP.NET Membership tables, stored procedures, etc. in the database project, synced my separate Entity Framework model to the same project and scripted some test and reference data (based on the build configuration) as well as some default ASP.NET users and roles. It then becomes a simple task to deploy this database to any environment in any state from within Visual Studio or a CI build. And by having the database project I can deploy to any environment directly from the CI build or Visual Studio or generate a SQL script to run at a later time, and I have more options than I could ever need for the database deployment including wrapping it in a transaction, creating the database from nothing or updating an existing database, blocking the script if data loss will occur, scripting pre and post deployment steps and many more.
Now my confusion comes from the fact that model first support seems to be slowly regressing and it appears the Magic Unicorn is the accepted mount from here on in. For example the Entity Framework Database Generation Power Pack is no longer supported in Visual Studio 2012 so I can no longer update my database project automatically from the model, there appears to have been no improvements to the EDMX designer or the model first development process and I’m now struggling to find any information about model first development via my search engine of choice where there seems to be a proliferation of Code First blog posts, tutorials and tweets.
Maybe I’m missing some pieces of the puzzle here, and I have only just moved to code first with my current project so I expect some time to adapt, but my general feeling so far is that I’ve taken a step back in terms of ease of development and testing, deployment processes and managing multiple environments. What do you think?