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];
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.
source share