Entity Framework: EF4 – update model from database ignores some changes
Posted by Jeroen Pluimers 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?
Do 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