带有部分更新的实体框架验证 [英] Entity Framework validation with partial updates
问题描述
我将 Entity Framework 5.0 与 DbContext 和 POCO 实体一起使用.有一个包含 3 个属性的简单实体:
I'm using Entity Framework 5.0 with DbContext and POCO entities. There's a simple entity containing 3 properties:
public class Record
{
public int Id { get; set; }
public string Title { get; set; }
public bool IsActive { get; set; }
}
Title 字段始终未修改,UI 只是显示它,不提供任何输入框来修改它.这就是将表单发送到服务器时 Title
字段设置为 null
的原因.
The Title field is always unmodified, and the UI simply displays it without providing any input box to modify it. That's why the Title
field is set to null
when the form is sent to the server.
以下是我告诉 EF 执行实体的部分更新的方法(仅限 IsActive
字段):
Here's how I tell EF to perform partial update of the entity (IsActive
field only):
public class EFRepository<TEntity>
{
...
public void PartialUpdate(TEntity entity, params Expression<Func<TEntity, object>>[] propsToUpdate)
{
dbSet.Attach(entity);
var entry = _dbContext.Entry(entity);
foreach(var prop in propsToUpdate)
contextEntry.Property(prop).IsModified = true;
}
}
和电话:
repository.PartialUpdate(updatedRecord, r => r.IsActive);
调用SaveChanges
方法,我得到DbEntityValidationException
,它告诉我,Title
是必需的.当我设置 dbContext.Configuration.ValidateOnSaveEnabled = false
时,一切正常.有什么方法可以避免在整个上下文中禁用验证并告诉 EF 不要验证未更新的属性?提前致谢.
Calling SaveChanges
method, I get the DbEntityValidationException
, that tells me, Title
is required. When I set dbContext.Configuration.ValidateOnSaveEnabled = false
, everything is OK.
Is there any way to avoid disabling validation on the whole context and to tell EF not to validate properties that are not being updated?
Thanks in advance.
推荐答案
如果您使用部分更新或存根实体(这两种方法都非常有效!)您不能使用全局 EF 验证,因为 它不尊重您的部分更改 - 它始终验证整个实体.使用默认验证逻辑,您必须通过调用以下代码将其关闭:
If you use partial updates or stub entities (both approaches are pretty valid!) you cannot use global EF validation because it doesn't respect your partial changes - it always validates whole entity. With default validation logic you must turn it off by calling mentioned:
dbContext.Configuration.ValidateOnSaveEnabled = false
并分别验证每个更新的属性.这应该有希望发挥作用,但我没有尝试,因为我根本不使用 EF 验证:
And validate every updated property separately. This should hopefully do the magic but I didn't try it because I don't use EF validation at all:
foreach(var prop in propsToUpdate) {
var errors = contextEntry.Property(prop).GetValidationErrors();
if (erros.Count == 0) {
contextEntry.Property(prop).IsModified = true;
} else {
...
}
}
如果你想更进一步,你可以尝试在你的上下文中覆盖 ValidateEntity
并重新实现验证,它验证整个实体或仅基于实体和 的状态验证选定的属性IsModified
属性状态 - 这将允许您对部分更新和存根实体使用 EF 验证.
If you want to go step further you can try overriding ValidateEntity
in your context and reimplement validation in the way that it validates whole entity or only selected properties based on state of the entity and IsModified
state of properties - that will allow you using EF validation with partial updates and stub entities.
EF 中的验证是恕我直言错误的概念 - 它在逻辑不属于的数据访问层中引入了额外的逻辑.它主要基于这样的想法:如果您将所需的验证规则放在导航属性上,则您始终使用整个实体甚至整个实体图.一旦您违反了这种方法,您总会发现将单一固定的验证规则硬编码到您的实体中是不够的.
Validation in EF is IMHO wrong concept - it introduces additional logic into data access layer where the logic doesn't belong to. It is mostly based on the idea that you always work with whole entity or even with whole entity graph if you place required validation rules on navigation properties. Once you violate this approach you will always find that single fixed set of validation rules hardcoded to your entities is not sufficient.
我在很长的待办事项中的一件事是调查验证如何影响 SaveChanges
操作的速度 - 我曾经在 EF4(EF4.1 之前)中拥有自己的验证 API,基于DataAnnotations 和它们的 Validator
类,由于性能很差,我很快就停止使用它了.
One of things I have in my very long backlog is to investigate how validation affects speed of SaveChanges
operation - I used to have my own validation API in EF4 (prior to EF4.1) based on DataAnnotations and their Validator
class and I stopped using it quite soon due to very poor performance.
使用本机 SQL 的解决方法与使用存根实体或关闭验证的部分更新具有相同的效果 = 您的实体仍未经过验证,但此外您的更改不是同一工作单元的一部分.
Workaround with using native SQL has same effect as using stub entities or partial updates with turned off validation = your entities are still not validated but in addition your changes are not part of same unit of work.
这篇关于带有部分更新的实体框架验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!