0

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:

enter image description here

Nimit Joshi
  • 1,026
  • 3
  • 19
  • 46

2 Answers2

0

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.

Giannis Paraskevopoulos
  • 18,261
  • 1
  • 49
  • 69
  • I have changed my code. But while trying to update the database from Package Manager Console, the same error is coming – Nimit Joshi Jul 21 '14 at 06:58
  • This won't fix the problem. It will just show you what the problem is. Try checking the Output window when this occurs. – Giannis Paraskevopoulos Jul 21 '14 at 07:01
  • There is only Build Succeed message on the Output screen – Nimit Joshi Jul 21 '14 at 07:29
  • Can you try what is suggested in the answer here: http://stackoverflow.com/questions/17169020/debug-code-first-entity-framework-migration-codes – Giannis Paraskevopoulos Jul 21 '14 at 07:36
  • sir, i got that. But would you please tell me where should i use the code which is described in the answer – Nimit Joshi Jul 21 '14 at 07:44
  • In one of the comments it says 'In the constructor of your configuration class.', so i would try there. Reading through the other answers though i see that it is suggested that you run this not through the package manager console, but running migrate.exe. Check the last answer of that link for more details. It worths the try. – Giannis Paraskevopoulos Jul 21 '14 at 07:49
0

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

Scott
  • 304
  • 1
  • 7