简体   繁体   English

当我尝试更新实体时,EF6引发EntityValidationErrors异常

[英]EF6 Throws Exception for EntityValidationErrors When I Try to Update Entity

I have a problem when I update entity in EF6. 我在EF6中更新实体时遇到问题。 The code looks like this: 代码如下:

public PICCOSSourceCost GetCOSSourceCost(int sourceCostID)
{
    return ERPContext.PICCOSSourceCost.Where(sc => sc.ID == sourceCostID && !sc.Deleted).FirstOrDefault();
}
public PICCOSSourceCost UpdateCOSSourceCost(PICCOSSourceCost sourceCost, bool saveChanges = true)
{
    var sc = GetCOSSourceCost(sourceCost.ID);
    if (sc == null)
    {
        throw new PICObjectNotFoundException<PICCOSSourceCost>(sourceCost, new List<string>()
        {
            nameof(PICCOSSourceCost.PICCOSSourceID),
            nameof(PICCOSSourceCost.PICCOSPriceTypeID),
            nameof(PICCOSSourceCost.Price),
            nameof(PICCOSSourceCost.EffectiveDate)
        });
    }

    sc.PICCOSSourceID = sourceCost.PICCOSSourceID;
    sc.PICCOSPriceTypeID = sourceCost.PICCOSPriceTypeID;
    sc.Price = sourceCost.Price;
    sc.EffectiveDate = sourceCost.EffectiveDate;
    sc.Deleted = sourceCost.Deleted;
    sc.CreatedBy = sourceCost.CreatedBy;
    sc.CreatedDate = sourceCost.CreatedDate;
    sc.LastModifiedBy = sourceCost.LastModifiedBy;
    sc.LastModifiedDate = sourceCost.LastModifiedDate;

    if (saveChanges) ERPContext.SaveChanges();

    return sc;
}

As you can see that the "GetCOSSourceCost" method get entity from EF. 如您所见,“ GetCOSSourceCost”方法从EF获取实体。 And the first parameter "sourceCost" in the "UpdateCOSSourceCost" method is passed in from FrontEnd, which is also got from EF6 . 从FrontEnd传入“ UpdateCOSSourceCost”方法中的第一个参数“ sourceCost”,这也是从EF6获取的 When I debug the code, there is an "System.Data.Entity.Validation.DbEntityValidationException" occurred. 当我调试代码时,发生了“ System.Data.Entity.Validation.DbEntityValidationException”。 I don't know why. 我不知道为什么 I think that it should be okay because I just get an entity object and just change its properties and save changes. 我认为应该没问题,因为我只是获得一个实体对象,然后更改其属性并保存更改。 Is it because there are two references to the same object? 是否因为有两个引用相同的对象? If I remove the property assignment code, the error will disappear. 如果删除属性分配代码,该错误将消失。

Validation failed for one or more entities. 一个或多个实体的验证失败。 See 'EntityValidationErrors' property for more details. 有关更多详细信息,请参见'EntityValidationErrors'属性。

Do you know why it throws this exception? 您知道为什么会引发此异常吗? Please help me. 请帮我。 Thank you so much! 非常感谢!

Based on this 基于此

http://mattrandle.me/viewing-entityvalidationerrors-in-visual-studio/ http://mattrandle.me/viewing-entityvalidationerrors-in-visual-studio/

You can use is a special debugger variable- $exception 您可以使用一个特殊的调试器变量- $exception

To view the EntityValidationErrors collection you can use below to show watch windows 要查看EntityValidationErrors集合,您可以在下面使用其显示监视窗口

((System.Data.Entity.Validation.DbEntityValidationException)$exception).EntityValidationErrors

Thank you for your help so much. 非常感谢您的帮助。 I have solved my issue by the following link: Validation failed for one or more entities. 我已通过以下链接解决了我的问题: 一个或多个实体的验证失败。 See 'EntityValidationErrors' property for more details For the sake of this link, I debugged and find the root cause of the entity validation error. 有关更多详细信息,请参见'EntityValidationErrors'属性。为了进行此链接,我调试并找到了实体验证错误的根本原因。 Some required fields are set to NULL value. 一些必填字段设置为NULL值。 The code from the answer is very useful: 答案中的代码非常有用:

try
{
    // Your code...
    // Could also be before try if you know the exception occurs in SaveChanges
    context.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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM