Entity Framework - Code First - ignore all but the specified properties

I work with some large classes that have many properties, and I do not want to ignore all the properties that I do not want to store in the database. Rather, is there anyway to ignore all the properties and specify only those that I want?

So instead

protected override void OnModelCreating(DbModelBuilder mb) { // code to ignore properties i don't want one at a time, ie 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 } 
+5
source share
1 answer

You can use reflection to invoke the Ignore method for all properties except the ones you need. This can be achieved by creating an extension method as follows:

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

First, we retrieve all the properties of the class that has the setter ( if you have more restrictions, you most provide them there ) and do not EntityTypeConfiguration<TEntityType> to the list of exceptions, then we call the Ignore class method EntityTypeConfiguration<TEntityType> for each property to ignore this property.

To call the Ignore method, we need to get the general type of the class, and then find the method of the Ignore class, then provide the general type of the Ignore method and finally call its corresponding argument.

The argument to the Ignore method is obtained by creating a lambda expression that selects the desired property from the TEntityType class.

After defining this extension class, you can call IgnoreAllExcept as follows:

 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 select class properties.

+4
source

All Articles