System.data.entity.validation.dbentityvalidationexception:'一个或多个实体的验证失败。 [英] System.data.entity.validation.dbentityvalidationexception: 'validation failed for one or more entities.

查看:313
本文介绍了System.data.entity.validation.dbentityvalidationexception:'一个或多个实体的验证失败。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.Data.Entity.Validation.DbEntityValidationException:'一个或多个实体的验证失败。有关详细信息,请参阅'EntityValidationErrors'属性。'



我尝试过:



这是我的Repository类文件



命名空间AutoBuggi.Repository

{

公共类CategoryRepository

{

public int CategoryId {get;私人集; } $ / $


//私人对象dbCategory;







public int SaveCategory(Models.CarCategory model)

{

try

{

using (var dbcontext = new AutoBuggiEntities())

{



var dbCategory = new AutoBuggi.CarCategory()

{

Id = Guid.NewGuid(),

CategoryId = model.CategoryId,

VariantName = model.VariantName

};

dbcontext.CarCategories.Add(dbCategory);

dbcontext.SaveChanges();

}

}





catch(例外情况)

{

抛出ex;

//返回int.Empty;

}



返回CategoryId;



}



public int DisplayAllCategory(Models.CarCategory model)

{

尝试

{

使用(var dbContext = new AutoBuggiEntities())

{

var categories = dbContext.CarCategories.ToList();

var dbCategory = new AutoBuggi.CarCategory()

{

Id = Guid.NewGuid (),

CategoryId = model.CategoryId,

VariantName = model.VariantName

};



dbContext.CarCategories.Add(dbCategory);

dbContext.SaveChanges() ;

}

}



catch(例外情况)

{< br $>


}



返回CategoryId;

}





}

}






$ b





这是我的HTML文件



@model AutoBuggi.ViewModels.CategoryViewModel



@ {

ViewBag.Title =创建类别;

}





@using(Html.BeginForm())

{

@ * @ Html.AntiForgeryToken()* @





CarCategory






@ Html.ValidationSummary(true,,new {@class =text-danger})< br $>







@ Html.LabelFor(model => model.CarCategory.CategoryId)

@ Html.EditorFor(model => model.CarCategory.CategoryId)

@ Html.ValidationMessageFor(model => model.CarCategory。 CategoryId)













@ Html.LabelFor(model => model.CarCategory .VariantName)

@ Html.EditorFor(model => model.CarCategory.VariantName)

@ Html.ValidationMessageFor(model => model.CarCategory.VariantName)



















}

解决方案

您是否尝试过验证数据库中每个属性的数据类型和长度?例如,您的 VariantName 属性在数据库中的长度为20个字符,但是您传递的数量可能超过可能引发该错误的字符数。



要验证错误的真正原因,您可以在尝试插入新记录时捕获 DbEntityValidationException 。例如:



 尝试 {
// 您在此处插入的代码
}
catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
{
Exception raise = dbEx;
foreach var validationErrors in dbEx.EntityValidationErrors)
{
foreach var validationError validationErrors.ValidationErrors)
{
string message = string .Format( {0}:{1}
validationErrors.Entry.Entity.ToString(),
validationError.ErrorMessage);
// 引发新的异常嵌套
// 当前实例为InnerException
raise = new InvalidOperationException (消息,提高);
}
}
throw raise;
}


System.Data.Entity.Validation.DbEntityValidationException: 'Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.'

What I have tried:

This is my Repository class file

namespace AutoBuggi.Repository
{
public class CategoryRepository
{
public int CategoryId { get; private set; }

// private object dbCategory;



public int SaveCategory(Models.CarCategory model)
{
try
{
using (var dbcontext = new AutoBuggiEntities())
{

var dbCategory = new AutoBuggi.CarCategory()
{
Id = Guid.NewGuid(),
CategoryId = model.CategoryId,
VariantName = model.VariantName
};
dbcontext.CarCategories.Add(dbCategory);
dbcontext.SaveChanges();
}
}


catch (Exception ex)
{
throw ex;
// return int.Empty;
}

return CategoryId ;

}

public int DisplayAllCategory(Models.CarCategory model)
{
try
{
using (var dbContext = new AutoBuggiEntities())
{
var categories = dbContext.CarCategories.ToList();
var dbCategory = new AutoBuggi.CarCategory()
{
Id = Guid.NewGuid(),
CategoryId = model.CategoryId,
VariantName = model.VariantName
};

dbContext.CarCategories.Add(dbCategory);
dbContext.SaveChanges();
}
}

catch(Exception ex)
{

}

return CategoryId;
}


}
}






This is my HTML file

@model AutoBuggi.ViewModels.CategoryViewModel

@{
ViewBag.Title = "Create Category";
}


@using (Html.BeginForm())
{
@*@Html.AntiForgeryToken()*@


CarCategory




@Html.ValidationSummary(true, "", new { @class = "text-danger" })




@Html.LabelFor(model => model.CarCategory.CategoryId)
@Html.EditorFor(model => model.CarCategory.CategoryId)
@Html.ValidationMessageFor(model => model.CarCategory.CategoryId)







@Html.LabelFor(model => model.CarCategory.VariantName)
@Html.EditorFor(model => model.CarCategory.VariantName)
@Html.ValidationMessageFor(model => model.CarCategory.VariantName)










}

解决方案

Have you tried validating the datatype and length for each of your property in the database? For example, your VariantName property has a length of 20 chars in your database but you are passing more than that which could potentially throws that error.

To verify the real cause of the error, you can catch the DbEntityValidationException when attempting to insert a new record. For example:

try{
   // your code for insert here
}
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;  
}  


这篇关于System.data.entity.validation.dbentityvalidationexception:'一个或多个实体的验证失败。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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