Saturday, December 20, 2014

disenchantment with Entity Framework 5 Code First stuff

I've rather lazily been tinkering on a GitHub project which uses the Entity Framework 5 code first stuff. I was all in love with this stuff because when you went to populate a database table and the table didn't exist, it just made the table! Well, I've finally gotten around to introducing a second database-hydrated object to my application and it turns out that it is not that simple after all. If you add an object in this manner to an empty database things work great, but if you try to add a second object you'll see an error like so:

Additional information: The model backing the 'CampaignContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).

 
 

In my case, my first object was called Person and my second object was called Campaign and the two don't even join to each other in any way, but the presence of a Persons table makes it impossible to add a Campaigns table. This and this touch on the official workarounds and this suggests that if you do things the way you're supposed to that you may have NHibernate-style lazy loading. I ended up just making two separate databases with the two separate objects and then using Red Gate SQL Compare 11 to make a SQL script that I could run to spin up a schema with both objects. That accommodated the creating of both objects, error-free. The whole inherit-from-DbMigration trick (the official workaround) sure looks to me like it breaks the Don't Repeat Yourself rule in that you have to update your class that inherits from DbMigration whenever you also update your objects. An example of this might be adding another field to an object and having to add another column to the database at the class you have inheriting from DbMigration. The first of the links I share in this post also eludes to the need to have a separate console application independent of your real application for spinning up the database. Dirty! What a dream-crushing shutdown.

I sink. I'm crestfallen. I don't know what I'm going to do when I actually bring relational data into my app.

No comments:

Post a Comment