I am using the EF 5.0 to Add Record to a table named Customer of the Northwind database, but always got this System.Data.Entity.Validation.DbEntityValidationException.Can anyone help?
Code as below:
private void Add_btn_Click(object sender, RoutedEventArgs e)
{
using (NorthwindEntities context = new NorthwindEntities())
{
Customer cust = new Customer()
{
CompanyName = "DotNetCurry",
ContactName = "Suprotim Agarwal"
};
context.Customers.Add(cust);
try
{
context.SaveChanges();
}
catch (DbEntityValidationException ex)
{
MessageBox.Show(ex.ToString());
}
}
}
Loading
Jignesh TrivediPosted Jun 6, 2013, 11:05 PM
hi,
this type of error occured due to validation fail on your entity.
check your entity's property value it is correct as per Business?
are you use DataAnnotation to validate entity?
you can also trace the validation messages
catch (DbEntityValidationException ex)
{
foreach (var validationErrors in ex.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Console.WriteLine("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
}
}
}
hope this will help you.