简体   繁体   English

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

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

I am working with some large classes that have lots of properties, and I don't want to have to ignore all the properties I don't want to save to the database. 我正在使用一些具有大量属性的大型类,我不想忽略我不想保存到数据库的所有属性。 Rather, is there anyway to ignore all properties and specify only the ones I want? 相反,无论如何都要忽略所有属性并仅指定我想要的属性?

So instead of this 所以不要这样

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);
}

I would have 我会

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

You can use reflection in order to call Ignore method for all properties except the ones that you need. 您可以使用反射来为所有属性调用Ignore方法,除了您需要的属性。 It can be achieved by creating an extension method like this: 可以通过创建这样的扩展方法来实现:

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;
    }
}

Firstly we extract all properties of the class that has a setter( if you have more constraints, you most provide them there ) and do not belong to the exception list, then we call Ignore method of the EntityTypeConfiguration<TEntityType> class for each property, in order to ignore that property. 首先,我们提取具有setter的类的所有属性( 如果你有更多的约束,你最多提供它们 )并且不属于异常列表,那么我们为每个属性调用EntityTypeConfiguration<TEntityType>类的Ignore方法,为了忽略那个属性。

To invoke the Ignore method, we need to obtain the generic class type, and then find the Ignore method of the class, then provide the generic type of the Ignore method, and finally invoke it by the appropriate argument. 要调用Ignore方法,我们需要获取泛型类类型,然后找到类的Ignore方法,然后提供Ignore方法的泛型类型,最后通过适当的参数调用它。

The argument of the Ignore method is obtained by creating a lambda expression that selects the desired property from the TEntityType class. Ignore方法的参数是通过创建一个lambda表达式来获得的,该表达式从TEntityType类中选择所需的属性。

After defining this extension class, you can call the IgnoreAllExcept like this: 定义此扩展类后,您可以像这样调用IgnoreAllExcept

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

You can also improve this method, by changing the except parameter to expressions that selects the properties of the class. 您还可以通过将except参数更改为选择类属性的表达式来改进此方法。

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

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