Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
using (sampleDbEntities sd = new sampleDbEntities())
{

    var t = new Product
    {
        Product_Name = d.Product_Name,
        Product_Name1 = d.Product_Name1,
        Product_Name2 = d.Product_Name2,
        Product_Name3 = d.Product_Name3,
        Product_Name4 = d.Product_Name4,
        Product_Name5 = d.Product_Name5,
        Product_Name6 = d.Product_Name6,
        Product_Name7 = d.Product_Name7,
        Product_Price = d.Product_Price,
        Product_Price1 = d.Product_Price1,
        Product_Price4 = d.Product_Price4,
        Product_Price2 = d.Product_Price1,
        Product_Price3 = d.Product_Price1,
        Product_Price5 = d.Product_Price5,
        Product_Price6 = d.Product_Price6,
        Product_Price7 = d.Product_Price7

    };
    sd.Products.Add(t);
    try
    {
        sd.SaveChanges();

    }
Posted
Comments
Khadar Babu Salijamala 18-Oct-15 6:59am    
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details... it was error messsage

I had the similar problem once. I solved it by debugging.
Use the debugger. Put a breakpoint on catch statement and see what is actually happening with your code.
You can get all the details from the DbEntityValidationException with the following code:
C#
try
{
    // Your code...
    db.SaveChanges();
}
catch (DbEntityValidationException e)
{
    foreach (var eve in e.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);
        }
    }
    throw;
}


-KR
 
Share this answer
 
C#
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFBScriptJ
{
    public class Class1
    {
        sampleDbEntities sd = new sampleDbEntities();
        static Product d = new Product();

        public static void Insert()
        {

            using (sampleDbEntities sd = new sampleDbEntities())
            {

                var t = new Product
                {
                    Product_Name = d.Product_Name,
                    Product_Name1 = d.Product_Name1,
                    Product_Name2 = d.Product_Name2,
                    Product_Name3 = d.Product_Name3,
                    Product_Name4 = d.Product_Name4,
                    Product_Name5 = d.Product_Name5,
                    Product_Name6 = d.Product_Name6,
                    Product_Name7 = d.Product_Name7,
                    Product_Price = d.Product_Price,
                    Product_Price1 = d.Product_Price1,
                    Product_Price4 = d.Product_Price4,
                    Product_Price2 = d.Product_Price2,
                    Product_Price3 = d.Product_Price3,
                    Product_Price5 = d.Product_Price5,
                    Product_Price6 = d.Product_Price6,
                    Product_Price7 = d.Product_Price7,

                };
                sd.Products.Add(t);
                try
                {
                    sd.SaveChanges();
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.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);
                        }
                    }
                    throw;
                }
            }
        }




        static void Main(string[] args)
        {
            // Product d = new Product();
            List<string> lines = System.IO.File.ReadAllLines(@"C:\khadar\Demo.mnk").ToList();
            int count = 0;
            int second = 8;
            int value = 0;

            for (int i = 8; i <= lines.Count; i = i + second)
            {
                count = i;
                if (count == 8)
                {
                    var firstEightLine = lines.Take(count).ToList();


                    foreach (var line in firstEightLine)
                    {
                        var content = line.Split(',');
                        if (line[0] == '1')
                        {

                            d.Product_Name = content[1].ToString();
                            d.Product_Price = content[2].ToString();

                        }
                        else if (line[0] == '2')
                        {
                            d.Product_Name1 = content[1].ToString();
                            d.Product_Price1 = content[2].ToString();

                        }
                        else if (line[0] == '3')
                        {
                            d.Product_Name2 = content[1].ToString();
                            d.Product_Price2 = content[2].ToString();

                        }
                        else if (line[0] == '4')
                        {
                            d.Product_Name3 = content[1].ToString();
                            d.Product_Price3 = content[2].ToString();

                        }

                        else if (line[0] == '5')
                        {
                            d.Product_Name4 = content[1].ToString();
                            d.Product_Price4 = content[2].ToString();

                        }
                        else if (line[0] == '6')
                        {
                            d.Product_Name5 = content[1].ToString();
                            d.Product_Price5 = content[2].ToString();

                        }
                        else if (line[0] == '7')
                        {
                            d.Product_Name6 = content[1].ToString();
                            d.Product_Price6 = content[2].ToString();

                        }

                        else if (line[0] == '8')
                        {
                            d.Product_Name7 = content[1].ToString();
                            d.Product_Price7 = content[2].ToString();

                        }
                    }
                    Insert();

                }
                else
                {
                    var secondline = lines.Skip(count - second).Take(8).ToList();

                    foreach (var line in secondline)
                    {
                        var content = line.Split(',');
                        if (line[0] == '1')
                        {

                            d.Product_Name = content[1].ToString();
                            d.Product_Price = content[2].ToString();

                        }
                        else if (line[0] == '2')
                        {
                            d.Product_Name1 = content[1].ToString();
                            d.Product_Price1 = content[2].ToString();

                        }
                        else if (line[0] == '3')
                        {
                            d.Product_Name2 = content[1].ToString();
                            d.Product_Price2 = content[2].ToString();

                        }
                        else if (line[0] == '4')
                        {
                            d.Product_Name3 = content[1].ToString();
                            d.Product_Price3 = content[2].ToString();

                        }

                        else if (line[0] == '5')
                        {
                            d.Product_Name4 = content[1].ToString();
                            d.Product_Price4 = content[2].ToString();

                        }
                        else if (line[0] == '6')
                        {
                            d.Product_Name5 = content[1].ToString();
                            d.Product_Price5 = content[2].ToString();

                        }
                        else if (line[0] == '7')
                        {
                            d.Product_Name6 = content[1].ToString();
                            d.Product_Price6 = content[2].ToString();

                        }

                        else if (line[0] == '8')
                        {
                            d.Product_Name7 = content[1].ToString();
                            d.Product_Price7 = content[2].ToString();

                        }


                        var mainvalue = count - second;
                    }


                    Insert();

                }



            }
        }
    }
}



</string>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900