Upgrading from Entity Framework 4 to 5 resulted in error

After upgrading to EF 5 I keep getting Validation failed for one or more entities. See 'EntityValidationErrors' property for more details' in a particular instance of my code.

It turns out I have a field that is a NVARCHAR nullable in the database and it marked has [Required] with Data annotation in a partial class. The field in question is set to null programmatically. This worked fine in EF4 as it was validated against the database model (NVARCHAR nullable).

I need to keep that field marked as [Required] because it also takes user inputs in other instances.

What are my options? Can I ignore that attribute/validation error right before SaveChanges()?

Answers


This is why it is strongly recommended that you use View models, rather than passing your entities directly to the view. Your view and data model have different requirements, and trying to use the same model with validation causes problems.

Instead, remove the required attribute from your data model and create a View model that has the required on it, then use something like AutoMapper to map between them.


Need Your Help

why DataContractSerializer is faster that xmlSerializer?

.net serialization datacontractserializer

Performance wise, why and how Datacontract serializer is better than xmlserializer ?

Bug in iOS 8.02? Breaks ALAssetsLibrary enumeration, any workaround?

ios ios8 alassetlibrary

I think AssetLibrary's group(album) enumeration api doesn't work anymore on ios 8.02, enumerateGroupsWithTypes:usingBlock: doesn't return the Recently Added group when enumerating all groups and r...