Reputation: 1046
I am creating an application from this link. Following is my DatabaseInitializer.cs
class:
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;
}
now i have to change the image entry. I have to insert the png images. For that I have enable the migration for the dbcontext class. Then in the Configuration.cs class i have inserted the following code:
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();
}
Now, When i am running the Update-Database
command in the Package Manager Console
i am getting the following error:
Upvotes: 0
Views: 269
Reputation: 304
The Product.cs model entity has two fields with the data annotation of [Required]. Since you haven't defined these values in the objects you are trying to AddOrUpdate(they are null), it is throwing an EntityValidationException.
Remove the annotations if they aren't required or add values in your Seed method. That should fix it.
You can also use the technique to require a debugger to be attached as jyparask suggested in the comment. It can be placed at the top of your seed method or any other method that is called prior to the Seed
Upvotes: 0
Reputation: 18411
In general this means you have violated one or more db constraints, such as a not null column or exceeded maximum length of a string column etc. So, in order to see what kind of errors you get, you may try the following while 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
{
}
}
}
This post, may not give a direct answer on your case, but adding it to your code will show you what is happening in your case.
Upvotes: 0