3

I work on a framework with EF. I want to get all ignored properties of an entity to build some special queries. How can I do it?

public class Customer
{
    public int Id { get; set; }
    public DateTime BirthDate { get; set; }
    public int Age { get; set; }
}

public class CustomerContext : DbContext
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Customer>().Ignore(customer => customer.Age);
        base.OnModelCreating(modelBuilder);
    }

    public DbSet<Customer> Customers { get; set; }
}

public static class DbContextExtensions
{
    public static List<string> GetIgnoredProperties(this DbContext context, string entityTypeName)
    {
         // ???
    }
}
3
  • Hmm, I read your question wrong. But, I will post an alternative anyway, using reflection and the [NotMapped] attribute. Apr 21, 2016 at 5:22
  • Wich version of EntityFramework are you using ? Apr 21, 2016 at 6:27
  • I would like to know what approach you took to get this working? Apr 21, 2016 at 11:00

2 Answers 2

5

I know this is not answering your original question, and in my comments I mentioned that you should use reflection, but that was only because I read your question wrong.

Here is an alternative using reflection, for if you do not come right.

If you assign the [NotMapped] attribute to the properties on your class that you would like to ignore, you could possibly retrieve all [NotMapped] properties using reflection. Below is an example of how this could be achieved.

var resultArray = yourClassInstance.GetType().GetProperties()
                            .Where(prop => Attribute.IsDefined(prop, typeof(NotMappedAttribute)));

Hope this helps you in some way.

1
  • 1
    The equivalent of [NotMapped] in Fluent API is to use the ignore as you did, but if you have your classes with properties already defined, I don't see any harm in placing the [NotMapped] attribute on the properties you would like to ignore. Fluent API ways and normal Attribute on your classes work well together. Apr 21, 2016 at 5:44
1

You can achieve what you want by calling the DbModelBuilder.Build. It will create a DbModel base on configuration setup by the DbModelBuilder. The DbModel expose a ConceptualModel that hold the types used by the context. The EdmModel hold each type that are declared in the context, and for each type, it hold the properties that has not been ignored by the DbModelBuilder during it's configuration. So, to achieve what you want, you have to intersect the properties of each entity type with those present in the EdmModel. It will give the delta between them, thefore the ignored properties. Here an example :

public class CustomerContext : DbContext
{
    private static IReadOnlyDictionary<Type, IReadOnlyCollection<PropertyInfo>> _ignoredProperties;
    /// Hold the ignored properties configured from fluent mapping
    public static IReadOnlyDictionary<Type, IReadOnlyCollection<PropertyInfo>> IgnoredProperties
    {
        get
        {
            return _ignoredProperties;
        }
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Customer>().Ignore(customer => customer.Age);

        // Build ignored properties only if they are not                
        if (_ignoredProperties == null)
        {                
            var model = modelBuilder.Build(this.Database.Connection);                
            var mappedEntityTypes = new Dictionary<Type, IReadOnlyCollection<PropertyInfo>>();
            foreach (var entityType in model.ConceptualModel.EntityTypes)
            {
                var type = Type.GetType(entityType.FullName);
                var typeProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                var mappedProperties = entityType.DeclaredProperties.Select(t => t.Name)
                    .Union(entityType.NavigationProperties.Select(t => t.Name));
                mappedEntityTypes.Add(type, new ReadOnlyCollection<PropertyInfo>(
                    typeProperties.Where(t => !mappedProperties.Contains(t.Name)).ToList()));
            }
            _ignoredProperties = new ReadOnlyDictionary<Type, IReadOnlyCollection<PropertyInfo>>(mappedEntityTypes);
        }

        base.OnModelCreating(modelBuilder);
    }

    public DbSet<Customer> Customers { get; set; }
}

The IgnoreProperties property is a singleton that will be initialized the first time you will use the context. It will be null before that, so will have to ensure that nothing use it until it's initialized. It's readonly, so you don't have to worrie about accidental clear of the collection. The entity type is used as key, and the value expose a collection that hold ignored properties. Example of use :

var properties = CustomerContext.IgnoredProperties[typeof(Customer)];

Cons :

With this approach is that the DbModel will be built twice, one time to gather the ignored properties, and second time by EntityFramework when the DbCompiledModel will be cached for futur ObjectContext creation. It can have an impact on the cold start of the DbContext, it means that the fist time you will execute a query over your context, it will be a bit slower. It will depend on the size of the DbContext. Warm queries should not suffer. OnModelCreating will be called once anyway.

Pros :

All changes made on de DbModelBuilder configuration will be automatically reflected in the IgnoredProperties property.

3
  • I would like your honest opinion. Do you think the pros outweigh to cons? +1 from me for your efforts. Apr 21, 2016 at 11:04
  • 1
    It will depend on how the application will work. If it has not too much downtime it worth the cost since only the warmup is impacted. On the over hand, if the application start and stop really often, like desktop application, it will not IMO. I'll update my answer to provide some benchmark. Thanks for your support by the way. Apr 21, 2016 at 11:22
  • I removed the benchmark temporarily since they are wrong, the OnModelCreateing wasn't called. Apr 21, 2016 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.