0

我有一个演示类“用户”,如下所示:

public partial class User {
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    [StringLength(30)]
    [Required]
    public string LoginName { get; set; }

    [StringLength(120)]
    [Required]
    [DataType(DataType.Password)]
    public string Pwd { get; set; }

    [StringLength(50)]
    public string Phone { get; set; }

    [StringLength(100)]
    public string WebSite { get; set; }

... ... }

如您所见,“登录名”和“密码”是“必填项”。

有时,我只想更新用户的“网站”,所以我这样做:

    public void UpdateUser(User user , params string[] properties) {
        this.rep.DB.Users.Attach(user);
        this.rep.DB.Configuration.ValidateOnSaveEnabled = false;
        var entry = this.rep.DB.Entry(user);
        foreach(var prop in properties) {
            var entProp = entry.Property(prop);
            //var vas = entProp.GetValidationErrors();
            entProp.IsModified = true;
        }
        this.rep.DB.SaveChanges();
        this.rep.DB.Configuration.ValidateOnSaveEnabled = true;
    }

像这样的参数“user”:new User(){ ID = 1, WebSite = "http://www.stackoverflow.com" }

注意,我没有指定“LoginName”和“Pwd”

此功能可以正常工作,但我不会将 ValidateOnSaveEnabled 设置为 false。

当 ValidateOnSaveEnabled 为真时,是否有任何方法仅验证“WebSite”?谢谢。

4

2 回答 2

1

我得到一个解决方案。首先定义PartialValidationManager:

public class PartialValidationManager {

    private IDictionary<DbEntityEntry , string[]> dics = new Dictionary<DbEntityEntry , string[]>();

    public void Register(DbEntityEntry entry , string[] properties) {
        if(dics.ContainsKey(entry)) {
            dics[entry] = properties;
        } else {
            dics.Add(entry , properties);
        }
    }

    public void Remove(DbEntityEntry entry) {
        dics.Remove(entry);
    }

    public bool IsResponsibleFor(DbEntityEntry entry) {
        return dics.ContainsKey(entry);
    }

    public void ValidateEntity(DbEntityValidationResult result) {
        var entry = result.Entry;
        foreach(var prop in dics[entry]){
            var errs = entry.Property(prop).GetValidationErrors();
            foreach(var err in errs) {
                result.ValidationErrors.Add(err);
            }
        }
    }
}

2、将此Manager添加到My DbContext:

public class XmjDB : DbContext {

    public Lazy<PartialValidationManager> PartialValidation = new Lazy<PartialValidationManager>();

    protected override System.Data.Entity.Validation.DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry , IDictionary<object , object> items) {
        if(this.PartialValidation.Value.IsResponsibleFor(entityEntry)) {
            var result = new DbEntityValidationResult(entityEntry , new List<DbValidationError>());
            this.PartialValidation.Value.ValidateEntity(result);
            return result;
        } else
            return base.ValidateEntity(entityEntry , items);
    }

……

更新方法:

    public void UpateSpecifyProperties(T t, params string[] properties) {
        this.DB.Set<T>().Attach(t);
        var entry = this.DB.Entry<T>(t);
        this.DB.PartialValidation.Value.Register(entry , properties);
        foreach(var prop in properties) {
            entry.Property(prop).IsModified = true;
        }
        this.DB.SaveChanges();
        this.DB.PartialValidation.Value.Remove(entry);
    }

好的,它工作正常。

于 2012-04-13T09:22:17.500 回答
1

据我所知,执行的SaveChanges验证总是验证整个实体。在您的代码中注释了对属性进行选择性验证的技巧,但它不是SaveChanges操作的一部分。

于 2012-04-12T10:00:18.753 回答