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

时间:2015-08-05 04:50:46

标签: c# entity-framework code-first ef-fluent-api

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

所以不是这个

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
}

1 个答案:

答案 0 :(得分:4)

您可以使用反射为所有属性调用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的类的所有属性(如果你有更多的约束,你最多提供它们)并且不属于异常列表,那么我们调用{{1每个属性的Ignore类的方法,以便忽略该属性。

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

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

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

IgnoreAllExcept

您还可以通过将protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<TestClass>().IgnoreAllExcept("Id", "Name"); } 参数更改为选择类属性的表达式来改进此方法。

相关问题