繁体   English   中英

实体框架 - 代码优先 - 忽略除指定的属性之外的所有属性

[英]Entity Framework - Code First - Ignore all properties except those specified

我正在使用一些具有大量属性的大型类,我不想忽略我不想保存到数据库的所有属性。 相反,无论如何都要忽略所有属性并仅指定我想要的属性?

所以不要这样

protected override void OnModelCreating(DbModelBuilder mb)
{
    // code to ignore properties i don't want one at a time, i.e.
    mb.Entity<Person>().Ignore(i => i.Name);
    mb.Entity<Person>().Ignore(i => i.Birthday);
}

我会

protected override void OnModelCreating(DbModelBuilder mb)
{
    // code to ignore all properties
    // code to include only properties I want
}

您可以使用反射来为所有属性调用Ignore方法,除了您需要的属性。 可以通过创建这样的扩展方法来实现:

public static class EntityTypeConfigurationExtentions
{
    public static EntityTypeConfiguration<TEntityType> IgnoreAllExcept<TEntityType>
       (this EntityTypeConfiguration<TEntityType> t, params string[] except)
        where TEntityType:class
    {
        var type = typeof(TEntityType);
        var properties = type.GetProperties();
        var dontIgnore = except ?? new string[0];
        //Here you can add more constraints on the class properties
        var toIgnore = properties.Where(x => !except.Contains(x.Name) && 
                                             x.SetMethod != null).ToList();
        foreach (var name in toIgnore)
        {
            var selector = GetIgnoreExpression<TEntityType>(name);
            MethodInfo genericMethod = GetIgnoreMethod<TEntityType>(name.PropertyType);
            genericMethod.Invoke(t, new object[] { selector });
        }
        return t;
    }
    private static MethodInfo GetIgnoreMethod<TEntityType>(Type propType)
    {
        var t = typeof(EntityTypeConfiguration<>);
        t = t.MakeGenericType(typeof(TEntityType));
        MethodInfo method = t.GetMethod("Ignore");
        MethodInfo genericMethod = method.MakeGenericMethod(propType);
        return genericMethod;
    }
    //This method creates the 'x=>x.PropertyName' expression for Ignore method
    private static Expression GetIgnoreExpression<TEntityType>(PropertyInfo prop)
    {
        ParameterExpression arg = Expression.Parameter(typeof(TEntityType), "x");
        MemberExpression property = Expression.Property(arg, prop.Name);
        var exp = Expression.Lambda(property, new ParameterExpression[] { arg });
        return exp;
    }
}

首先,我们提取具有setter的类的所有属性( 如果你有更多的约束,你最多提供它们 )并且不属于异常列表,那么我们为每个属性调用EntityTypeConfiguration<TEntityType>类的Ignore方法,为了忽略那个属性。

要调用Ignore方法,我们需要获取泛型类类型,然后找到类的Ignore方法,然后提供Ignore方法的泛型类型,最后通过适当的参数调用它。

Ignore方法的参数是通过创建一个lambda表达式来获得的,该表达式从TEntityType类中选择所需的属性。

定义此扩展类后,您可以像这样调用IgnoreAllExcept

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
       modelBuilder.Entity<TestClass>().IgnoreAllExcept("Id", "Name");
}

您还可以通过将except参数更改为选择类属性的表达式来改进此方法。

暂无
暂无

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

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