DbEntityValidationException升级到EF 4.1后 [英] DbEntityValidationException After Upgrading to EF 4.1

查看:140
本文介绍了DbEntityValidationException升级到EF 4.1后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我决定升级到EF 4.1(从CTP5)。我使用的是Code First。不幸的是,每当我尝试运行该项目时,都会得到这个例外:


System.Data.Entity.Validation.DbEntityValidationException:
一个或多个
实体的验证失败。有关详细信息,请参阅EntityValidationErrors
属性。


EntityValidationErrors属性并没有真正说明有关实体的任何内容导致异常。 Nether实际上是Stack Trace。但是,异常被抛出在它上面的Context.SaveChanges()里面Seed()覆盖(在初始化器类中)的行上。



经过一些调试和注释掉一些代码,我认为这与User,Item和Rating类有关。以下是这些类的代码:

  public class User 
{
public int Id {get;组; }
public string昵称{get;组; }
public string FirstName {get;组; }
public string LastName {get;组; }
public string Email {get;组; }
public string Password {get;组; }
public float Credits {get;组; }
public float PromotionalCredits {get;组; }
public string电话{get;组; }
public string Mobile {get;组; }
public double RatingAverage {get;组; }
public string ProfileImage {get;组; }
public int? DefaultAddressId {get;组; }
[ForeignKey(DefaultAddressId)]
public virtual Address DefaultAddress {get;组; }
public virtual ICollection< Address>地址{get;组; }
public virtual ICollection< Role>角色{get;组; }
public virtual ICollection< Comment>评论{get;组; }
public virtual ICollection< Item>物品{get;组; }
public virtual ICollection< Bid>投标{get;组; }
public virtual ICollection< CreditCard>信用卡{get;组; }
public virtual ICollection< Message> ReceivedMessages {get;组; }
public virtual ICollection< Message> SentMessages {get;组; }
public virtual ICollection< Item> WatchList {get;组; }
public virtual ICollection< Item> ViewList {get;组; }
public virtual ICollection< Rating> OwnRatings {get;组; }
public virtual ICollection< Rating>评分为其他组;
}

public class Item
{
public int Id {get;组; }
public string标题{get;组; }
public string描述{get;组; }
public float StartingPrice {get;组; }
public float? BidIncrement {get;组; }
public DateTime StartDate {get;组; }
public DateTime EndDate {get;组; }
public int Status {get;组; }
[ForeignKey(Status)]
public virtual ItemStatus ItemStatus {get;组; }
public virtual Address PickupAddress {get;组; }
public virtual User User {get;组; }
public virtual ChildCategory类别{get;组; }
public virtual ICollection< Comment>评论{get;组; }
public virtual ICollection< Image>图片{get;组; }
public virtual ICollection< Bid>投标{get;组; }
public virtual ICollection< User>守望者{get;组; }
public virtual ICollection< User>查看用户{get;组; }
public virtual ICollection< Tag>标签{get;组; }
}

这里是我将一些测试数据添加到数据库的代码使用Seed()覆盖:

  var user1 = new User 
{
FirstName =John ,
LastName =Smith,
昵称=J.Smith,
Email =john.smith@live.com,
Password =myPassword,
Mobile =01542367,
电话=824225523,
ViewList =新列表< Item>(),
WatchList = new List< Item>(),
ReceivedMessages = new List< Message>(),
SentMessages = new List< Message>(),
Roles = new List< Role>(),
RatingsForOthers = new List& (),
OwnRatings = new List< Rating>(),
Bids = new List< Bid>(),
Credits = 600,
PromotionalCredits = 20,
ProfileImage =http://localhost/Content/images/temp/default_profile.jpg,
评论=新列表<注释>(),
Addresses = new List< Address>
{
new Address
{
Area =(from c in districts.ElementAt(2).Cities.ElementAt(0).Areas
其中c.GoogleName。等于(Sirhmoul)
选择c).First(),
Details =我的街道地址,
Name =Home address
}
}
};


var add1 =(from c in user1.Addresses
其中c.Name.Equals(Home Address)
select c).First();


var item1 = new Item
{
标题=HTC Desire,
描述=Lorem Ipsum只是虚拟文本,
StartingPrice = 400f,
User = user1,
EndDate = DateTime.Now.AddDays(10),
StartDate = DateTime.Now,
BidIncrement = 3f,
Status = 1,
Bids = new List< Bid>(),
注释=新列表<注释>(),
PickupAddress = add1,
Images = new List&图像>
{
new Image
{
描述=某些图片描述,
Path =http:// localhost:2732 / images / temp / dummyItem.png ,
Rank = 1
},
new Image
{
描述=some image2 description,
Path =http:// localhost :2732 / Content / images / temp / dummyItem.png,
Rank = 2
},
new Image
{
描述=some image3 description
Path =http:// localhost:2732 / Content / images / temp / dummyItem.png,
Rank = 3
}
},
ViewingUsers =新列表< User>(),
WatchingUsers = new List< User>(),
标签=新列表< Tag>()

};


var electronics =(从c在类别
其中c.Name.Equals(Electronics)
选择c).First();

var cellPhones =(from c in electronics.Children
where c.Name.Equals(Cell Phones& PDA's)
select c).First();
cellPhones.Items.Add(item1);

更新:(清理代码并将其替换为更新版本)



现在我确定它实际上是添加正在抛出异常的项目的代码。我注释出了创建一个项目的代码,并将User实例自己添加到上下文中,然后我运行该项目,它的工作正常。那么,我正在创建一个项目的代码正在发生什么?



注意:升级到EF 4.1之前,我没有这个问题



任何建议?



谢谢。

解决方案

最后,这是解决这个问题的方法: EF 4.1 RTW更改为默认值MaxLength代码优先


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.

解决方案

And finally, here's the solution to this issue: EF 4.1 RTW Change to Default MaxLength in Code First

这篇关于DbEntityValidationException升级到EF 4.1后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆