Entity Framework: EF4 – update model from database ignores some changes
Posted by jpluimers on 2011/09/13
Almost a year ago there was a post on the MSDN forums titled EF4 – update model from database ignores some changes.
These are the changes it ignores that I found so far:
- Column renames
- Column data type changes
- Changed foreign key relations
Have you found others?
Which EF versions are worse/better in this respect?
Note that the EF support in Visual Studio 2010 does not warn you if the the model is incompatible with your database.
You will get errors like this at run-time:
System.InvalidOperationException: The 'Size' property on 'ParentEntity' could not be set to a 'Int64' value. You must set this property to a non-null value of type 'Int32'. at System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal) at System.Data.Common.Internal.Materialization.Shaper.GetPropertyValueWithErrorHandling[TProperty](Int32 ordinal, String propertyName, String typeName) at lambda_method(Closure , Shaper ) at System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet) at lambda_method(Closure , Shaper ) at System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper) at System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
The workaround is simple but tedious:
- check all the errors, (so you need a thorough testing scheme in place)
- make a list of all the entities involved
- for each entity:
- note all the manual changes you did
- delete it from the model
- add it to the model
- re-apply the manual changes you did
–jeroen






bill bates said
Same old problem. The code first fluent guys are messing up the database first. Why doesn’t Microsoft get the one way working correctly first before doing all the fluent Rad, go and fumble stuff? It looks like the regen of the entity model doesn’t work for ef-4.1. maybe they will get this act together for Juneau.
Craig said
I think you’ll find the SSDL is updated, but the CSDL isn’t. EF designer won’t change CSDL once it’s created. So you can manually update CSDL or redo the whole thing from scratch.