EF: Validation failing on update when using lazy-loaded, required properties

后端 未结 8 2046
栀梦
栀梦 2020-12-04 15:03

Given this extremely simple model:

public class MyContext : BaseContext
{
    public DbSet Foos { get; set; }
    public DbSet Bars { g         


        
8条回答
  •  星月不相逢
    2020-12-04 15:56

    Here's a semi-acceptable work-around:

    var errors = this.context.GetValidationErrors();
    foreach (DbEntityValidationResult result in errors) {
        Type baseType = result.Entry.Entity.GetType().BaseType;
        foreach (PropertyInfo property in result.Entry.Entity.GetType().GetProperties()) {
            if (baseType.GetProperty(property.Name).GetCustomAttributes(typeof(RequiredAttribute), true).Any()) {
                property.GetValue(result.Entry.Entity, null);
            }
        }
    }
    

提交回复
热议问题