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);
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
Any suggestions?
Thank you.
I noticed that your scalar properties are not marked
virtual
, but your navigation properties are. It's too early in the morning to figure why that would produce the error you described, but it's probably worth changing that to see if it helps.And finally, here's the solution to this issue: EF 4.1 RTW Change to Default MaxLength in Code First