Get ignored properties in Entity Framework

c# ef-code-first entity-framework entity-framework-5 entity-framework-6

Question

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)
    {
         // ???
    }
}
1
3
4/21/2016 5:13:42 AM

Popular Answer

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.

2
4/21/2016 6:02:06 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow