DbEntityValidationException升级到EF 4.1后

时间:2011-03-28 07:52:37

标签: c# asp.net-mvc entity-framework entity-framework-4 ef-code-first

今天我决定升级到EF 4.1(来自CTP5)。我正在使用Code First。不幸的是,每当我尝试运行项目时,我都会遇到这个异常:

  

System.Data.Entity.Validation.DbEntityValidationException:   验证失败了一个或多个   实体。请参阅'EntityValidationErrors'   物业详情。

EntityValidationErrors属性并未真正说明导致异常的实体。虚空确实做了Stack Trace。但是,在Seed()覆盖(在初始化器类中)中的context.SaveChanges()行中抛出异常。

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

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; }
}

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

    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);

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

现在我确定它实际上是添加抛出异常的项目的代码。我注释掉了创建项目的代码,并将User实例单独添加到上下文中,然后我运行了项目,它运行得很好。那么,我正在创建项目的代码是怎么回事?

注意:在升级到EF 4.1之前我没有遇到此问题

有什么建议吗?

谢谢。

2 个答案:

答案 0 :(得分:2)

最后,这是此问题的解决方案:EF 4.1 RTW Change to Default MaxLength in Code First

答案 1 :(得分:0)

我注意到您的标量属性未标记为virtual,但您的导航属性是。{现在上午太清楚为什么会产生你所描述的错误,但是可能值得改变它以确定它是否有帮助。