0

我正在使用实体框架 5 和 SQL Server。我正在插入数据,但得到以下信息:

public partial class TestContext : DbContext

    public override int SaveChanges()
    {
        this.ApplyRules(); 
        return base.SaveChanges();
    }


Validation failed for one or more entities. 
See 'EntityValidationErrors' property for more details.

有人可以告诉我如何找到有关验证错误的更多信息吗?

我查看了异常详细信息,但没有提供任何详细信息

4

1 回答 1

0

这是我使用的代码:

public override int SaveChanges()
{
    this.ApplyRules(); 
    try
    {
        base.SaveChanges();
    }
    catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
    {
        Exception raise = dbEx;
        foreach (var validationErrors in dbEx.EntityValidationErrors)
        {
            foreach (var validationError in validationErrors.ValidationErrors)
            {
                string message = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage);
                //raise a new exception inserting the current one as the InnerException
                raise = new InvalidOperationException(message , raise);
            }
        }
        throw raise;
    }
}
于 2013-07-23T14:30:30.403 回答