C# 实体框架验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/191143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Entity Framework Validation
提问by Micah
I'm getting ready to start a new project and I've been researching the entity framework. My question is what is the best strategy for validating the entities? Other projects I've worked on have used attributes for most of the validation, but obviously this is not possible in the entity framework. Is the only way to do this by handling the partial methods in the property setters? All advice is much appreciated.
我准备开始一个新项目,我一直在研究实体框架。我的问题是验证实体的最佳策略是什么?我参与过的其他项目在大多数验证中都使用了属性,但显然这在实体框架中是不可能的。通过处理属性设置器中的部分方法来做到这一点的唯一方法是什么?非常感谢所有建议。
采纳答案by smaclell
I have not actually used the Entity framework before but a quick search indicates that you have several options.
我之前没有实际使用过实体框架,但快速搜索表明您有多种选择。
1) Validate at another layer in your application
1) 在应用程序的另一层进行验证
Always an option, I just thought I would throw it out there explicitly.
总是一个选择,我只是想我会明确地把它扔掉。
2) Hook into the OnChanged events of the Entity then perform validation
2)挂钩实体的OnChanged事件然后执行验证
Likely brittle and would become confusing/slow after if you have many different properties things that can change for each entity.
如果您有许多不同的属性可以为每个实体更改,则可能很脆弱并且会变得混乱/缓慢。
3) Implement partial methods to validate property changes
3) 实现部分方法来验证属性更改
According to thispost and thiswalkthrough there are partial methods available for validation. This seems like your best option as it is not very intrusive and you can selectively implement the validation you want.
根据这篇文章和本演练,有部分方法可用于验证。这似乎是您的最佳选择,因为它不是很具有侵入性,您可以有选择地实施您想要的验证。
I hope that helps. Good luck.
我希望这有帮助。祝你好运。
回答by aogan
If you use ASP.NET MVC, then you could use Validation Application Block or the System.ComponentModel.DataAnnotations. The articles Using Data Annotationsand Using Application Blockshow how to do them using Linq, but the usage with entity-framework should be similiar.
如果您使用 ASP.NET MVC,那么您可以使用验证应用程序块或 System.ComponentModel.DataAnnotations。文章Using Data Annotations和Using Application Block展示了如何使用 Linq 进行操作,但实体框架的用法应该是相似的。
回答by Shimmy Weitzhandler
In .NET 4, there is going to be out-the-box validation support in Entity-Framework.
在 .NET 4 中,Entity-Framework 将提供开箱即用的验证支持。
Check out: http://blogs.msdn.com/adonet/archive/2010/01/13/introducing-the-portable-extensible-metadata.aspx
查看:http: //blogs.msdn.com/adonet/archive/2010/01/13/introducing-the-portable-extensible-metadata.aspx
So don't work to hard on implementing too complex validation logic...
所以不要努力实现太复杂的验证逻辑......
回答by jbe
If you are using WPF or Windows Forms then you might implement the IDataErrorInfointerface.
如果您使用 WPF 或 Windows 窗体,那么您可能会实现IDataErrorInfo接口。
The BookLibrarysample application of the WPF Application Framework (WAF)project shows how entities created by the Entity Framework can be validated.
WPF 应用程序框架 (WAF)项目的BookLibrary示例应用程序展示了如何验证实体框架创建的实体。
回答by Rob
We have overrident the object context and intercept the SaveChanges() method
我们已经覆盖了对象上下文并拦截了 SaveChanges() 方法
public abstract class ValidationObjectContext : ObjectContext{
...
public override int SaveChanges(SaveOptions options){
ValidateEntities();
return base.SaveChanges(options);
}
}
That way the validation is left until the last minute before the connections are made but after you are (expecting) to be happy with the graph and ready to commit, (as opposed to other options to validation on any change, since some complex rules like those we have are only valid after several properties are set.). We have two levels of validation, Basic Property validation, things like string length, nullability etc. And Business Logic validation, which might require checking rules across multiple objects, possibly hitting the database to confirm.
这样,验证会保留到建立连接之前的最后一分钟,但在您(期望)对图形感到满意并准备好提交之后,(与验证任何更改的其他选项相反,因为一些复杂的规则,例如我们拥有的那些只有在设置了几个属性后才有效。)。我们有两个级别的验证,基本属性验证,诸如字符串长度、可空性等。以及业务逻辑验证,这可能需要跨多个对象检查规则,可能需要访问数据库进行确认。
回答by Kamran
Consider implementing IValidatableObject in your entities.
考虑在您的实体中实现 IValidatableObject。