实体验证失败。请参阅' EntityValidationErrors'属性

时间:2014-07-21 06:29:47

标签: c# entity-framework

我正在从this链接创建一个应用程序。以下是我的DatabaseInitializer.cs课程:

private static List<Product> GetProducts()
    {
        var products = new List<Product>{
            new Product
            {
                ProductID = 1,
                ProductName="HTC",
                ProductDescription="HTC Mobiles are very nice",
                ImagePath="htc.jpg",
                UnitPrice=25000,
                CategoryID=1
            },
            new Product
            {
                ProductID = 2,
                ProductName="Nokia",
                ProductDescription="Mokia Lumia Mobiles are very smart",
                ImagePath="nokia.jpg",
                UnitPrice=30000,
                CategoryID=1
            },
            new Product
            {
                ProductID = 3,
                ProductName="Samsung",
                ProductDescription="Samdung Mobiles are very great",
                ImagePath="samsung.jpg",
                UnitPrice=20000,
                CategoryID=1
            },
            new Product
            {
                ProductID = 4,
                ProductName="Apple",
                ProductDescription="Apple Laptops are very superb",
                ImagePath="apple.jpg",
                UnitPrice=80000,
                CategoryID=2
            },
            new Product
            {
                ProductID = 5,
                ProductName="Dell",
                ProductDescription="Dell Laptops are very nice",
                ImagePath="dell.jpg",
                UnitPrice=45000,
                CategoryID=2
            },
            new Product
            {
                ProductID = 6,
                ProductName="Lenovo",
                ProductDescription="Lenovo Laptops are very great",
                ImagePath="lenovo.jpg",
                UnitPrice=50000,
                CategoryID=2
            },
            new Product
            {
                ProductID = 7,
                ProductName="Cannon",
                ProductDescription="Cannon Cameras are very nice",
                ImagePath="cannon.jpg",
                UnitPrice=25000,
                CategoryID=3
            },
            new Product
            {
                ProductID = 8,
                ProductName="Nikon",
                ProductDescription="Nikon Cameras are superb",
                ImagePath="nikon.jpg",
                UnitPrice=35000,
                CategoryID=3
            },
            new Product
            {
                ProductID = 9,
                ProductName="Sony",
                ProductDescription="Sony Cameras are very great",
                ImagePath="sony.jpg",
                UnitPrice=40000,
                CategoryID=3
            },
            new Product
            {
                ProductID = 10,
                ProductName="Creative",
                ProductDescription="Creative Speakers are very nice",
                ImagePath="creative.jpg",
                UnitPrice=25000,
                CategoryID=4
            },
            new Product
            {
                ProductID = 11,
                ProductName="Jbl",
                ProductDescription="Jbl Speakers are great",
                ImagePath="jbl.jpg",
                UnitPrice=45000,
                CategoryID=4
            },
            new Product
            {
                ProductID = 12,
                ProductName="Philips",
                ProductDescription="Philips Speakers are awesome",
                ImagePath="philips.jpg",
                UnitPrice=35000,
                CategoryID=4
            },
        };
        return products;
    }

现在我必须更改图像条目。我必须插入png图像。为此,我启用了dbcontext类的迁移。然后在Configuration.cs类中插入了以下代码:

 protected override void Seed(SamplePayPalApp.Models.ProductDbContext context)
    {
        var New_Products = new List<Product>
        {

          new Product{ImagePath="htc.png"},
          new Product{ImagePath="nokia.png"},
          new Product{ImagePath="samsung.png"},
          new Product{ImagePath="apple.png"},
          new Product{ImagePath="dell.png"},
          new Product{ImagePath="lenovo.png"},
          new Product{ImagePath="cannon.png"},
          new Product{ImagePath="nikon.png"},
          new Product{ImagePath="sony.png"},
          new Product{ImagePath="creative.png"},
          new Product{ImagePath="jbl.png"},
          new Product{ImagePath="philips.png"},
       };

        New_Products.ForEach(np => context.Products.AddOrUpdate(p => p.ImagePath, np));
        context.SaveChanges();
    }

现在,当我在Update-Database中运行Package Manager Console命令时,我收到以下错误:

enter image description here

2 个答案:

答案 0 :(得分:0)

通常,这意味着您违反了一个或多个数据库约束,例如非空列或超出字符串列的最大长度等。因此,为了查看您获得的错误类型,您可以尝试以下操作而SaveChanges:

try
{
    context.SaveChanges();
}
catch(Exception ex)
{
    try
        {
            foreach (var eve in ((DbEntityValidationException)ex).EntityValidationErrors)
            {
                Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                    eve.Entry.Entity.GetType().Name, eve.Entry.State);
                foreach (var ve in eve.ValidationErrors)
                {
                    Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                        ve.PropertyName, ve.ErrorMessage);
                }
            }
        }
        catch
        {
        }

    }
}

这篇文章可能无法直接回复您的案例,但将其添加到您的代码中会告诉您案例中发生了什么。

答案 1 :(得分:0)

Product.cs模型实体有两个字段,数据注释为[Required]。由于您没有在尝试AddOrUpdate的对象中定义这些值(它们为null),因此它抛出了EntityValidationException。

如果不需要注释,请删除注释,或在Seed方法中添加值。那应该解决它。

您还可以使用该技术要求将调试器附加为注释中建议的jyparask。它可以放在种子方法的顶部或种子

之前调用的任何其他方法