EF数据库模型验证 [英] EF Model Validation against database

查看:405
本文介绍了EF数据库模型验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用EF 5模型验证来避免数据库中的重复值,所以我使用的模型类如下:

  [Table(MeasureUnits)] 
public class MeasureUnit:IValidatableObject
{
public int MeasureUnitId {get;组; }

public string符号{get;组; }

public string Name {get;组; }

public IEnumerable&ValidationResult>验证(ValidationContext validationContext)
{
using(MeasureUnitRepository rep = new MeasureUnitRepository())
{
MeasureUnit measureUnit = rep.FindDuplicateBySymbol(this);

如果(measureUnit!= null)
yield返回新的ValidationResult(
还有另一个单位带有这个符号,你不能复制,
new [] {Symbol});
}
}

存储库类创建DbContext,实现IDisposable,具有找到重复的逻辑,它都按照预期的方式工作。



但是,使用调试器,我意识到验证对于每次插入或更新都执行两次,因此存储库(和DbContext)被实例化和处理两次。



除此之外,还有一个DbContext生活在控制器中,但是没有找到在模型类,除了在模型的构造函数中包含DbContext,但我觉得这不是正确的解决方案。



有没有更好的正确的方式来实现这个验证



提前感谢

解决方案

当你必须去到数据库,那么你需要使用 DbContext DbContext 具有一个称为 ValidateEntity 。请参阅这篇文章:实体框架验证



我将代码放在另一个答案中此处



更多关于我如何在MVC中组织验证这里。



另外,在存储库中实例化一个上下文可能会导致你的悲伤。存储库将需要共享上下文。您可以将上下文作为您的工作单元,并将其传递到构造函数中的存储库中,也可以将上下文包含在您自己的工作单元中,并将其传递到。


I want to use EF 5 model validation to avoid duplicate values in the database, so I'm using a model class like this:

[Table("MeasureUnits")]
public class MeasureUnit : IValidatableObject
{
    public int MeasureUnitId { get; set; }

    public string Symbol { get; set; }

    public string Name { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        using (MeasureUnitRepository rep = new MeasureUnitRepository())
        {
            MeasureUnit measureUnit = rep.FindDuplicateBySymbol(this);

            if (measureUnit != null)
                yield return new ValidationResult(
                    "There is another unit with this symbol, you can't duplicate it", 
                    new[] { "Symbol" });
        }
    }

The repository class creates the DbContext, implements IDisposable, has the logic to find the duplicate and it all works just as intended.

However, using the debugger I realized the validation is performed twice for every insert or update, so the repository (and DbContext) gets instantiated and disposed twice also.

Besides that, there is another DbContext living in the controller but just don't find the way to use it inside the model class, other than including the DbContext in the model's constructor, but I feel it's not the right solution.

Is there a better o "right" way to achieve this validation?

Thanks in advance.

解决方案

When you have to go to the database then you need to use DbContext and DbContext has an Overridable method called ValidateEntity. See this article: Entity Framework Validation.

I put the code I use in another answer here

And more about how I've structured the validation in MVC here.

Also, instantiating a context inside your repository is likely to cause you grief. The repositories will need to share a context. You could treat the context as your unit of work and pass it into the repository in the constructor, or you could wrap the context in your own unit of work and pass that in.

这篇关于EF数据库模型验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆