find out the exact entity causing an exception in entity framework

前端 未结 2 799
悲&欢浪女
悲&欢浪女 2021-02-02 18:39

The entity framework gives me generic messages in the exception without telling me the exact entity and the attribute which caused the error. How do I get more information about

2条回答
  •  粉色の甜心
    2021-02-02 18:50

    You need to write tests for repositories and in the base class for your tests:

    try
    {
        DbContext.SaveChanges();
    }
    catch (DbEntityValidationException e)
    {
        e.EntityValidationErrors.SelectMany(error => error.ValidationErrors).ToList().ForEach(
        item => Console.WriteLine("{0} - {1}", item.PropertyName, item.ErrorMessage));
        throw;
    }
    

提交回复
热议问题