繁体   English   中英

验证EF Code First + Ria Services中的NotMapped属性

[英]Validation of NotMapped properties in EF Code First + Ria Services

我正在使用RIA Services和EF Code First创建一个应用程序,在该应用程序中,实体可以使用自定义属性进行扩展(每个可扩展实体都有一个属性包,基本上与属性实体是一对多的关系)。

而且,已经编写了一种代码生成机制,该机制为每个此类“扩展”属性生成“普通” c#属性包装器。 因此,整个机制对开发人员而言更加透明

    [NotMapped]
    public string Version
    {
        get
        {
            return this.GetProperty(PropertyNameVersionConst) == null
                        ? null
                        : this.GetProperty(PropertyNameVersionConst).StringValue;
        }
        set
        {
            this.SetProperty(PropertyNameVersionConst, value);          
        }
    }

问题是-我可以在([NotMapped])属性上使用验证属性吗? 乍一看,我看不出为什么不应该这样做。

[NotMapped]
[Required]
public string Version{...}

我遇到了这样一个问题,即即使属性HAS设置为非null值,也会针对具有Required属性的此类属性抛出验证异常(在SaveChanges()上)。

好了,在属性中添加了虚拟修饰符后,它便开始正常工作

[NotMapped]
public virtual string Version
{
    get
    {
        return this.GetProperty(PropertyNameVersionConst) == null
                    ? null
                    : this.GetProperty(PropertyNameVersionConst).StringValue;
    }
    set
    {
        this.SetProperty(PropertyNameVersionConst, value);          
    }
}

任何人都可以详细说明这一点:)?

暂无
暂无

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

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