SaveChanges()上的验证错误 [英] Validation Error on SaveChanges()

查看:233
本文介绍了SaveChanges()上的验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Asp.net mvc Web应用程序中具有以下Action方法:-

I have the following Action method inside my Asp.net mvc web application:-

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(SDJoin sdj, FormCollection formValues)
{
    Try
    {
        //code goes here
        repository.InsertOrUpdateSD(sdj.StorageDevice, User.Identity.Name, assetid);
        repository.Save();
    }
    catch (Exception ex)
    {
        //code goes here
    }
    PopulateViewBagData();
    return View(sdj);
}

它将调用以下方法:-

public void InsertOrUpdateSD(TMSStorageDevice sd, string username, long assetid)
{
    var resource = entities.Resources.AsNoTracking().SingleOrDefault(a => a.RESOURCEID == assetid);
    if (sd.TMSStorageDeviceID == default(int))
    {
        // New entity
        int technologyypeID = GetTechnologyTypeID("Storage Device");
        Technology technology = new Technology
        {
            IsDeleted = true,
            IsCompleted = false,
            TypeID = technologyypeID,
            Tag = "SD" + GetTagMaximumeNumber2(technologyypeID).ToString(),
            StartDate = DateTime.Now,
            IT360ID = assetid
        };

        InsertOrUpdateTechnology(technology);
        Save();

        sd.TMSStorageDeviceID = technology.TechnologyID;
        tms.TMSStorageDevices.Add(sd);
    }
}

我的模型课如下:-

public partial class TMSStorageDevice
{
    public int TMSStorageDeviceID { get; set; }
    public string Name { get; set; }
    public Nullable<decimal> size { get; set; }
    public int RackID { get; set; }
    public string CustomerName { get; set; }
    public string Comment { get; set; }
    public byte[] timestamp { get; set; }

    public virtual Technology Technology { get; set; }
    public virtual TMSRack TMSRack { get; set; }
}

但是如果我调用Create action方法,我将得到以下异常:-

but if i call the Create action method i will get the following exception:-

System.Data.Entity.Validation.DbEntityValidationException was caught
  HResult=-2146232032
  Message=Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
  Source=EntityFramework
  StackTrace:
       at System.Data.Entity.Internal.InternalContext.SaveChanges()
       at System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
       at System.Data.Entity.DbContext.SaveChanges()
       at TMS.Models.Repository.Save() in c:\Users\Administrator\Documents\Visual Studio 2012\Projects\TMS\TMS\Models\Repository.cs:line 1926
       at TMS.Controllers.StorageDeviceController.Create(SDJoin sdj, FormCollection formValues) in c:\Users\Administrator\Documents\Visual Studio 2012\Projects\TMS\TMS\Controllers\StorageDeviceController.cs:line 160
  InnerException:

任何人都可以建议出什么问题了,因为我仔细检查了我的代码,并且一切正常. 谢谢

Can anyone advice what is wrong, as i double check my code and every thing should work fine ? Thanks

推荐答案

您没有显示Save()方法,但是如果您可以向其中添加这样的代码,则会得到一个包含所有详细信息的异常.重新寻找

You haven't shown the Save() method but if you can add code like this to it you'll get an exception that contains all the details you're looking for

try
{
    _context.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 nesting
            // the current instance as InnerException
            raise = new InvalidOperationException(message, raise);
        }
    }
    throw raise;
}

这篇关于SaveChanges()上的验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆