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

use*_*633 5 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);
}
Run Code Online (Sandbox Code Playgroud)

我会

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

Tah*_*ooy 7

您可以使用反射来调用除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;
    }
}
Run Code Online (Sandbox Code Playgroud)

首先,我们提取具有setter的类的所有属性(如果您有更多约束,您通常会在那里提供它们)并且不属于例外列表,然后我们为每个属性调用Ignore该类的方法EntityTypeConfiguration<TEntityType>,以便忽略该属性财产。

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

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

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

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

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