简体   繁体   English

实体框架5.0 ValidateOnSaveEnabled无法正常工作?

[英]Entity Framework 5.0 ValidateOnSaveEnabled not working?

public void Delete<TEntity>(TEntity entity) where TEntity : class, IEntity
{
    this.Configuration.ValidateOnSaveEnabled = false;
    if (!this.Set<TEntity>().Local.Any(d => d.Id == entity.Id))
    {
        this.Set<TEntity>().Attach(entity);
    }
    this.Set<TEntity>().Remove(entity);
    SaveChanges();
    this.Configuration.ValidateOnSaveEnabled = true;
}

The code above throw an exception on the SaveChanges(). 上面的代码在SaveChanges()上抛出异常。

[DbUpdateException: Null value for non-nullable member. Member: 'Name'.]

How can I remove the validation when removing an Entity? 删除实体时如何删除验证?

Not sure if this is the problem but if you are deleting entity using a stub object then try setting the DbEntityEntry.State to deleted instead. 不确定这是否是问题,但如果您使用存根对象删除实体,请尝试将DbEntityEntry.State设置为删除。 I'm unsure if you even need to turn validateOnSaveEnabled off. 我不确定你是否需要关闭validateOnSaveEnabled

public void Delete<TEntity>(TEntity entity) where TEntity : class, IEntity
{
    this.Configuration.ValidateOnSaveEnabled = false;
    TEntity alreadyAttached = this.Set<TEntity>().Local
                                  .FirstOrDefault(d=>d.Id == entity.Id);
    if(alreadyAttached != null) entity = alreadyAttached;
    this.Entry<TEntity>(entity).State = EntityState.Deleted;
    SaveChanges();
    this.Configuration.ValidateOnSaveEnabled = true;
 }

Edit 编辑

What is written above is may not be entirely correct. 上面所写的内容可能并不完全正确。 As pointed out by Pawel it is a DbUpdateException not DbEntityValidationException . 正如Pawel所指出的,它是DbUpdateException而不是DbEntityValidationException The reason that the above may work for you is the handling of the case if the entity is attached. 以上可能对您有用的原因是如果附加实体,则处理案件。

if(!this.Set<TEntity>().Local.Any(d=>d.Id == entity.Id))
{
    this.Set<TEntity>().Attach(entity);
}
this.Set<TEntity>().Remove(entity);

Consider the case here if the entity is already attached. 如果实体已经附加,请考虑此处的情况。 Not the passed instance of entity (you are just comparing ids) but another instance representing the same entity. 不是传递的实体实例(您只是比较ID),而是另一个表示同一实体的实例。 Now consider if you have modified this other entity somewhere in code before this point, it would be tracked by the context as modified. 现在考虑一下,如果你在此之前在代码中的某处修改了这个其他实体,它将被上下文跟踪为已修改。 You then attempt to remove your passed instance of entity -- the one that is not being tracked -- resulting in a no-op. 然后,您尝试删除传递的实体实例 - 未被跟踪的实体 - 导致无操作。 You then call savechanges and and BAM! 然后你调用savechanges和BAM! DbUpdateException as EF attempts to update the entity you thought you deleted. DbUpdateException因为EF会尝试更新您认为已删除的实体。

The exception you are seeing is not related to validation what you have is DbUpdateException and not a validation exception). 您看到的异常与验证无关DbUpdateException而不是验证异常)。 The exception message says that then Name property of the entity being saved is null while the column in the database is not nullable. 异常消息说,然后保存的实体的Name属性为null,而数据库中的列不可为空。 (If validation was turned on it you would probably get a validation error - is it why you were trying to disable it at all?) (如果启用了验证,您可能会收到验证错误 - 这是您尝试禁用它的原因吗?)

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

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