当第一个项目有实体验证错误时保存项目列表时出错
Error saving a list of items when first item have entity validation error
我有一个通用业务 class,我用它来处理我的数据 mysql 5.6 和 entity framework 6。我创建了一个从 table 并创建一个对象列表,以便我可以调用业务 class 并保存或更新目标数据库中的记录。
//part of import method
using (var business = new Business<Customer>(context))
{
foreach (var sourceCustomer in sourceCustomersList)
{
var customer = business.GetData().ToList().Find(x=>x.OriginalId == sourceCustomer.Id) ?? new Customer();
customer.Name = sourceCustomer.Name; // for example, this property is required
customer.Fone = sourceCustomer.Fone;
customer.City = sourceCustomer.City;
customer.State = sourceCustomer.State);
customer.Active = sourceCustomer.Active;
if (customer.Id > 0)
{
business.Update(Customer);
}
else
{
customer.SourceId = sourceCustomer.Id;
business.Insert(fornecedor);
}
}
}
public class Business<T> : IBusiness<T>, IDisposable
{
protected ESistemContext Context;
public Business(ESistemContext context)
{
Context = context;
}
public virtual bool Insert(T entity)
{
try
{
Context.Entry(entity).State = EntityState.Added;
Context.SaveChanges();
return true;
}
catch (Exception exception)
{
return false;
}
}
public virtual bool Update(T entity)
{
try
{
Context.Entry(entity).State = EntityState.Modified;
Context.SaveChanges();
return true;
}
catch (Exception exception)
{
return false;
}
}
public void Dispose()
{
if (Context == null)
return;
Context.Dispose();
Context = null;
}
}
我的问题是:当我对一个项目有验证错误时(名称 属性 为空值),我尝试处理的所有其他项目都出现相同的验证错误(即使它们有一个名称属性 正确填写)调用 context.SaveChanges() 业务时 class 调用异常并显示相同的验证错误。
经过大量搜索,找到了问题的解决方案。在 catch 块中添加了以下代码行,上下文现在将条目释放为新值,然后进行新验证。
Context.Entry(entity).State = EntityState.Detached;
我有一个通用业务 class,我用它来处理我的数据 mysql 5.6 和 entity framework 6。我创建了一个从 table 并创建一个对象列表,以便我可以调用业务 class 并保存或更新目标数据库中的记录。
//part of import method
using (var business = new Business<Customer>(context))
{
foreach (var sourceCustomer in sourceCustomersList)
{
var customer = business.GetData().ToList().Find(x=>x.OriginalId == sourceCustomer.Id) ?? new Customer();
customer.Name = sourceCustomer.Name; // for example, this property is required
customer.Fone = sourceCustomer.Fone;
customer.City = sourceCustomer.City;
customer.State = sourceCustomer.State);
customer.Active = sourceCustomer.Active;
if (customer.Id > 0)
{
business.Update(Customer);
}
else
{
customer.SourceId = sourceCustomer.Id;
business.Insert(fornecedor);
}
}
}
public class Business<T> : IBusiness<T>, IDisposable
{
protected ESistemContext Context;
public Business(ESistemContext context)
{
Context = context;
}
public virtual bool Insert(T entity)
{
try
{
Context.Entry(entity).State = EntityState.Added;
Context.SaveChanges();
return true;
}
catch (Exception exception)
{
return false;
}
}
public virtual bool Update(T entity)
{
try
{
Context.Entry(entity).State = EntityState.Modified;
Context.SaveChanges();
return true;
}
catch (Exception exception)
{
return false;
}
}
public void Dispose()
{
if (Context == null)
return;
Context.Dispose();
Context = null;
}
}
我的问题是:当我对一个项目有验证错误时(名称 属性 为空值),我尝试处理的所有其他项目都出现相同的验证错误(即使它们有一个名称属性 正确填写)调用 context.SaveChanges() 业务时 class 调用异常并显示相同的验证错误。
经过大量搜索,找到了问题的解决方案。在 catch 块中添加了以下代码行,上下文现在将条目释放为新值,然后进行新验证。
Context.Entry(entity).State = EntityState.Detached;