如何使用实体框架工作在db中插入代码时解决一个或多个实体的验证失败 [英] How to resolve validation fail for one or more entities in inserting code in db using entity frame work
本文介绍了如何使用实体框架工作在db中插入代码时解决一个或多个实体的验证失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用(sampleDbEntities sd = new sampleDbEntities())
{
var t = new 产品
{
Product_Name = d.Product_Name,
Product_Name1 = d.Product_Name1,
Product_Name2 = d.Product_Name2,
Product_Name3 = d.Product_Name3,
Product_Name4 = d.Product_Name4,
Product_Name5 = d.Product_Name5,
Product_Name6 = d.Product_Name6,
Product_Name7 = d.Product_Name7,
Product_Price = d.Product_Price,
Product_Price1 = d.Product_Price1,
Product_Price4 = d.Product_Price4,
Product_Price2 = d.Product_Price1,
Product_Price3 = d.Product_Price1,
Product_Price5 = d.Product_Price5,
Product_Price6 = d.Product_Price6,
Product_Price7 = d.Product_价格7
};
sd.Products.Add(t);
尝试
{
sd.SaveChanges();
}
解决方案
我曾遇到过类似的问题。我通过调试解决了这个问题。
使用调试器
。在catch语句上放置一个断点,看看代码实际发生了什么。
您可以使用以下代码从DbEntityValidationException
获取所有详细信息:
尝试
{
// 您的代码......
db.SaveChanges();
}
catch (DbEntityValidationException e)
{
foreach ( var eve in e.EntityValidationErrors)
{
Console.WriteLine ( 状态\{1} \中类型为\{0} \的实体具有以下验证错误:,
eve.Entry.Entity.GetType()。Name,eve.Entry.State);
foreach ( var ve in eve.ValidationErrors)
{
Console.WriteLine( - Property:\{ 0} \,错误:\{1} \,
ve.PropertyName,ve.ErrorMessage);
}
}
throw ;
}
-KR
< span class =code-keyword>使用系统;
使用 System.Collections.Generic;
使用 System.Data.Entity.Validation;
使用 System.Data.SqlClient;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
命名空间 EFBScriptJ
{
public class Class1
{
sampleDbEntities sd = new sampleDbEntities();
static 产品d = new Product();
public static void 插入()
{
使用(sampleDbEntities sd = new sampleDbEntities())
{
var t = new 产品
{
Product_Name = d.Product_Name,
Product_Name1 = d.Product_Name1,
Product_Name2 = d.Product_Name2,
Product_Name3 = d.Product_Name3,
Product_Name4 = d.Product_Name4,
Product_Name5 = d.Product_Name5,
Product_Name6 = d.Product_Name6,
Product_Name7 = d.Product_Name7,
Product_Price = d.Product_Price,
Product_Price1 = d.Product_Price1,
Product_Price4 = d.Product_Price4,
Product_Price2 = d.Product_Price2,
Product_Price3 = d.Product_Price3,
Product_Price5 = d.Product_Price5,
Product_Price6 = d.Product_Price6,
Product_Price7 = d.Product_Price7,
};
sd.Products.Add(t);
尝试
{
sd.SaveChanges();
}
catch (DbEntityValidationException e)
{
foreach ( var eve in e.EntityValidationErrors)
{
Console.WriteLine ( 状态\{1} \中类型为\{0} \的实体具有以下验证错误:,
eve.Entry.Entity.GetType()。Name,eve.Entry.State);
foreach ( var ve in eve.ValidationErrors)
{
Console.WriteLine( - Property:\{ 0} \,错误:\{1} \,
ve.PropertyName,ve.ErrorMessage);
}
}
throw ;
}
}
}
静态 void Main( string [] args)
{
// 产品d =新产品();
列表< string> lines = System.IO.File.ReadAllLines( @ C:\ khadar \ Demo.mnk)ToList();
int count = 0 ;
int second = 8 ;
int value = 0 ;
for ( int i = 8 ; i < = lines.Count; i = i + second)
{
count = i;
if (count == 8 )
{
var firstEightLine = lines.Take(count).ToList();
foreach ( var line in firstEightLine)
{
var content = line.Split(' ,');
if (行[ 0 ] == ' 1')
{
d.Product_Name = content [ 1 跨度>]的ToString();
d.Product_Price = content [ 2 ]。ToString();
}
其他 如果(行[ 0 ] == ' 2')
{
d.Product_Name1 = content [ 1 ]。ToString();
d.Product_Price1 = content [ 2 ]。ToString();
}
其他 如果(行[ 0 ] == ' 3')
{
d.Product_Name2 = content [ 1 ]。ToString();
d.Product_Price2 = content [ 2 ]。ToString();
}
其他 如果(行[ 0 ] == ' 4')
{
d.Product_Name3 = content [ 1 ]。ToString();
d.Product_Price3 = content [ 2 ]。ToString();
}
其他 如果(行[ 0 ] == ' 5')
{
d.Product_Name4 = content [ 1 ]。ToString();
d.Product_Price4 = content [ 2 ]。ToString();
}
其他 如果(行[ 0 ] == ' 6')
{
d.Product_Name5 = content [ 1 ]。ToString();
d.Product_Price5 = content [ 2 ]。ToString();
}
其他 如果(行[ 0 ] == ' 7')
{
d.Product_Name6 = content [ 1 ]。ToString();
d.Product_Price6 = content [ 2 ]。ToString();
}
其他 如果(行[ 0 ] == ' 8')
{
d.Product_Name7 = content [ 1 ]。ToString();
d.Product_Price7 = content [ 2 ]。ToString();
}
}
Insert();
}
else
{
var secondline = lines.Skip(count - second).Take( 8 )。ToList();
foreach ( var line in secondline)
{
var content = line.Split(' ,');
if (行[ 0 ] == ' 1')
{
d.Product_Name = content [ 1 跨度>]的ToString();
d.Product_Price = content [ 2 ]。ToString();
}
其他 如果(行[ 0 ] == ' 2')
{
d.Product_Name1 = content [ 1 ]。ToString();
d.Product_Price1 = content [ 2 ]。ToString();
}
其他 如果(行[ 0 ] == ' 3')
{
d.Product_Name2 = content [ 1 ]。ToString();
d.Product_Price2 = content [ 2 ]。ToString();
}
其他 如果(行[ 0 ] == ' 4')
{
d.Product_Name3 = content [ 1 ]。ToString();
d.Product_Price3 = content [ 2 ]。ToString();
}
其他 如果(行[ 0 ] == ' 5')
{
d.Product_Name4 = content [ 1 ]。ToString();
d.Product_Price4 = content [ 2 ]。ToString();
}
其他 如果(行[ 0 ] == ' 6')
{
d.Product_Name5 = content [ 1 ]。ToString();
d.Product_Price5 = content [ 2 ]。ToString();
}
其他 如果(行[ 0 ] == ' 7')
{
d.Product_Name6 = content [ 1 ]。ToString();
d.Product_Price6 = content [ 2 ]。ToString();
}
其他 如果(行[ 0 ] == ' 8')
{
d.Product_Name7 = content [ 1 ]。ToString();
d.Product_Price7 = content [ 2 ]。ToString();
}
var mainvalue = count - second;
}
Insert();
}
}
}
}
}
< / string > ; 跨度>
using (sampleDbEntities sd = new sampleDbEntities())
{
var t = new Product
{
Product_Name = d.Product_Name,
Product_Name1 = d.Product_Name1,
Product_Name2 = d.Product_Name2,
Product_Name3 = d.Product_Name3,
Product_Name4 = d.Product_Name4,
Product_Name5 = d.Product_Name5,
Product_Name6 = d.Product_Name6,
Product_Name7 = d.Product_Name7,
Product_Price = d.Product_Price,
Product_Price1 = d.Product_Price1,
Product_Price4 = d.Product_Price4,
Product_Price2 = d.Product_Price1,
Product_Price3 = d.Product_Price1,
Product_Price5 = d.Product_Price5,
Product_Price6 = d.Product_Price6,
Product_Price7 = d.Product_Price7
};
sd.Products.Add(t);
try
{
sd.SaveChanges();
}
解决方案
I had the similar problem once. I solved it by debugging.
Use thedebugger
. Put a breakpoint on catch statement and see what is actually happening with your code.
You can get all the details from theDbEntityValidationException
with the following code:
try { // Your code... db.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; }
-KR
using System; using System.Collections.Generic; using System.Data.Entity.Validation; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EFBScriptJ { public class Class1 { sampleDbEntities sd = new sampleDbEntities(); static Product d = new Product(); public static void Insert() { using (sampleDbEntities sd = new sampleDbEntities()) { var t = new Product { Product_Name = d.Product_Name, Product_Name1 = d.Product_Name1, Product_Name2 = d.Product_Name2, Product_Name3 = d.Product_Name3, Product_Name4 = d.Product_Name4, Product_Name5 = d.Product_Name5, Product_Name6 = d.Product_Name6, Product_Name7 = d.Product_Name7, Product_Price = d.Product_Price, Product_Price1 = d.Product_Price1, Product_Price4 = d.Product_Price4, Product_Price2 = d.Product_Price2, Product_Price3 = d.Product_Price3, Product_Price5 = d.Product_Price5, Product_Price6 = d.Product_Price6, Product_Price7 = d.Product_Price7, }; sd.Products.Add(t); try { sd.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; } } } static void Main(string[] args) { // Product d = new Product(); List<string> lines = System.IO.File.ReadAllLines(@"C:\khadar\Demo.mnk").ToList(); int count = 0; int second = 8; int value = 0; for (int i = 8; i <= lines.Count; i = i + second) { count = i; if (count == 8) { var firstEightLine = lines.Take(count).ToList(); foreach (var line in firstEightLine) { var content = line.Split(','); if (line[0] == '1') { d.Product_Name = content[1].ToString(); d.Product_Price = content[2].ToString(); } else if (line[0] == '2') { d.Product_Name1 = content[1].ToString(); d.Product_Price1 = content[2].ToString(); } else if (line[0] == '3') { d.Product_Name2 = content[1].ToString(); d.Product_Price2 = content[2].ToString(); } else if (line[0] == '4') { d.Product_Name3 = content[1].ToString(); d.Product_Price3 = content[2].ToString(); } else if (line[0] == '5') { d.Product_Name4 = content[1].ToString(); d.Product_Price4 = content[2].ToString(); } else if (line[0] == '6') { d.Product_Name5 = content[1].ToString(); d.Product_Price5 = content[2].ToString(); } else if (line[0] == '7') { d.Product_Name6 = content[1].ToString(); d.Product_Price6 = content[2].ToString(); } else if (line[0] == '8') { d.Product_Name7 = content[1].ToString(); d.Product_Price7 = content[2].ToString(); } } Insert(); } else { var secondline = lines.Skip(count - second).Take(8).ToList(); foreach (var line in secondline) { var content = line.Split(','); if (line[0] == '1') { d.Product_Name = content[1].ToString(); d.Product_Price = content[2].ToString(); } else if (line[0] == '2') { d.Product_Name1 = content[1].ToString(); d.Product_Price1 = content[2].ToString(); } else if (line[0] == '3') { d.Product_Name2 = content[1].ToString(); d.Product_Price2 = content[2].ToString(); } else if (line[0] == '4') { d.Product_Name3 = content[1].ToString(); d.Product_Price3 = content[2].ToString(); } else if (line[0] == '5') { d.Product_Name4 = content[1].ToString(); d.Product_Price4 = content[2].ToString(); } else if (line[0] == '6') { d.Product_Name5 = content[1].ToString(); d.Product_Price5 = content[2].ToString(); } else if (line[0] == '7') { d.Product_Name6 = content[1].ToString(); d.Product_Price6 = content[2].ToString(); } else if (line[0] == '8') { d.Product_Name7 = content[1].ToString(); d.Product_Price7 = content[2].ToString(); } var mainvalue = count - second; } Insert(); } } } } } </string>
这篇关于如何使用实体框架工作在db中插入代码时解决一个或多个实体的验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文