Answered by:
DbEntityValidationException After Upgrading to EF 4.1

Question
-
Hi everyone,
Today I decided to upgrade to EF 4.1 (from CTP5). I am using Code First. Unfortunately, whenever I try to run the project I get this exception:
> System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
The EntityValidationErrors property doesn't really say anything about the entities that are causing the exception. Nether does the Stack Trace actually. But, the exception is thrown on the line where it says context.SaveChanges() inside the Seed() override (in the initializer class).
After some debugging and commenting out some code, I think it's something to do with the User, Item and Rating classes. Below is the code for those classes:
public class User { public int Id { get; set; } public string Nickname { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } public string Password { get; set; } public float Credits { get; set; } public float PromotionalCredits { get; set; } public string Telephone { get; set; } public string Mobile { get; set; } public double RatingAverage { get; set; } public string ProfileImage { get; set; } public int? DefaultAddressId { get; set; } [ForeignKey("DefaultAddressId")] public virtual Address DefaultAddress { get; set; } public virtual ICollection<Address> Addresses { get; set; } public virtual ICollection<Role> Roles { get; set; } public virtual ICollection<Comment> Comments { get; set; } public virtual ICollection<Item> Items { get; set; } public virtual ICollection<Bid> Bids { get; set; } public virtual ICollection<CreditCard> CreditCard { get; set; } public virtual ICollection<Message> ReceivedMessages { get; set; } public virtual ICollection<Message> SentMessages { get; set; } public virtual ICollection<Item> WatchList { get; set; } public virtual ICollection<Item> ViewList { get; set; } public virtual ICollection<Rating> OwnRatings { get; set; } public virtual ICollection<Rating> RatingsForOthers { get; set; } } public class Item { public int Id { get; set; } public string Title { get; set; } public string Description { get; set; } public float StartingPrice { get; set; } public float? BidIncrement { get; set; } public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } public int Status { get; set; } [ForeignKey("Status")] public virtual ItemStatus ItemStatus { get; set; } public virtual Address PickupAddress { get; set; } public virtual User User { get; set; } public virtual ChildCategory Category { get; set; } public virtual ICollection<Comment> Comments { get; set; } public virtual ICollection<Image> Images { get; set; } public virtual ICollection<Bid> Bids { get; set; } public virtual ICollection<User> WatchingUsers { get; set; } public virtual ICollection<User> ViewingUsers { get; set; } public virtual ICollection<Tag> Tags { get; set; } }
And here's the code where I'm adding some test data to the database using the Seed() override:
var user1 = new User { FirstName = "John", LastName = "Smith", Nickname = "J.Smith", Email = "john.smith@live.com", Password = "myPassword", Mobile = "01542367", Telephone = "824225523", ViewList = new List<Item>(), WatchList = new List<Item>(), ReceivedMessages = new List<Message>(), SentMessages = new List<Message>(), Roles = new List<Role>(), RatingsForOthers = new List<Rating>(), OwnRatings = new List<Rating>(), Bids = new List<Bid>(), Credits = 600, PromotionalCredits = 20, ProfileImage = "http://localhost/Content/images/temp/default_profile.jpg", Comments = new List<Comment>(), Addresses = new List<Address> { new Address { Area = (from c in districts.ElementAt(2).Cities.ElementAt(0).Areas where c.GoogleName.Equals("Sirhmoul") select c).First(), Details = "my address in the street", Name = "Home Address" } } }; var add1 = (from c in user1.Addresses where c.Name.Equals("Home Address") select c).First(); var item1 = new Item { Title = "HTC Desire", Description = "Lorem Ipsum is simply dummy text", StartingPrice = 400f, User = user1, EndDate = DateTime.Now.AddDays(10), StartDate = DateTime.Now, BidIncrement = 3f, Status = 1, Bids = new List<Bid>(), Comments = new List<Comment>(), PickupAddress = add1, Images = new List<Image> { new Image { Description = "some image description", Path = "http://localhost:2732/images/temp/dummyItem.png", Rank = 1 }, new Image { Description = "some image2 description", Path = "http://localhost:2732/Content/images/temp/dummyItem.png", Rank = 2 }, new Image { Description = "some image3 description", Path = "http://localhost:2732/Content/images/temp/dummyItem.png", Rank = 3 } }, ViewingUsers = new List<User>(), WatchingUsers = new List<User>(), Tags = new List<Tag>() }; var electronics = (from c in categories where c.Name.Equals("Electronics") select c).First(); var cellPhones = (from c in electronics.Children where c.Name.Equals("Cell Phones & PDA's") select c).First(); cellPhones.Items.Add(item1);
Any suggestions?
Thank you.
Sunday, March 27, 2011 10:06 PM
Answers
-
Dear Jackie,
Thank you for reporting this bug. Here's the solution (by the ADO.NET team): http://blogs.msdn.com/b/adonet/archive/2011/03/29/ef-4-1-rtw-change-to-default-maxlength-in-code-first.aspx
Many thanks for the team! :)
- Marked as answer by KassemD Monday, April 4, 2011 8:13 AM
Monday, April 4, 2011 8:13 AM
All replies
-
UPDATE: (cleaned up the code and replaced it with the updated version)
Now I'm sure it's actually the code that is adding the item which is throwing the exception. I commented out the code that creates an item and added the User instance on its own to the context, and then I ran the project and it worked just fine. So, what's going on with the code in which I'm creating an item?
Note: I did not have this issue prior to the upgrade to EF 4.1
Monday, March 28, 2011 8:45 AM -
I still haven't managed to resolve this issue. Is there a way to disable entity validation when calling SaveChanges()? This seems to be a bug in EF 4.1 I think...Tuesday, March 29, 2011 4:21 PM
-
Hello KassemD,
Welcome to the EF Forum!
According to your description, I think there's a question: have you inspected form variables being posted to ensure each matches the database defined type? And probably one of your required fields has a null value. Could you please check your values and properties?
And more, the way to disable automatic validation invoked from DbContext.SaveChanges(). This can be done by setting DbContextConfiguration.ValidateOnSaveEnabled configuration setting to false (by default this value is set to true) like this:
ctx.Configuration.ValidateOnSaveEnabled = false;
I hope this can help you.
Have a nice day,
Jackie Sun [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Wednesday, March 30, 2011 8:23 AM -
Thanks for your reply. But I repeat, I did not have this issue before upgrading to EF 4.1 so I really suspect this is a bug. But anyway, I'm also thinking this could be caused by the DateTime properties (StartDate and EndDate). I'm not really submitting the data using a form, I'm doing this manually inside the Seed() override (just for testing purposes). I have a question though... What are the required fields? Only scalar attributes and not navigational properties, right?Wednesday, March 30, 2011 8:34 PM
-
Hi KassemD ,
Thanks for your reply!
I mean that if you have any time you could check the datatype of fields and whether they allow nullable, etc…And about if it is a EF 4.1 RC bug, in my mind, the EF 4.1 introduces two new features: The DbContext API and Code First. First I must thank you for finding this issue, and I will do a further check, if it is really a bug I will report to the EF Design Team, and help them fix it.
Thank you so much!
Have a nice day,
Jackie Sun [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Proposed as answer by Jackie-Sun Monday, April 4, 2011 3:21 AM
Thursday, March 31, 2011 3:03 AM -
Dear Jackie,
Thank you for reporting this bug. Here's the solution (by the ADO.NET team): http://blogs.msdn.com/b/adonet/archive/2011/03/29/ef-4-1-rtw-change-to-default-maxlength-in-code-first.aspx
Many thanks for the team! :)
- Marked as answer by KassemD Monday, April 4, 2011 8:13 AM
Monday, April 4, 2011 8:13 AM -
You're welcome :)
Good day,
Jackie Sun [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Monday, April 4, 2011 8:44 AM