Entity Framework 6 : ignore property on basetype for all derived types

.net c# entity-framework-6 odata


I must disregard the property.State on abstract class BaseEntity nevertheless, I can't seem to make this work without utilizing the[NotMappedAttribute] , however if I utilize the attribute, the OData API ignores the property as well.

To test this, I've created the following github project: https://github.com/nurf3d/EntityFrameworkDerivedTypesIgnoreProperiesTest/tree/master.

Chain of inheritance:

public abstract class BaseEntity
    public int ID { get; set; }

    public byte[] Rowversion { get; set; }

    public State State { get; set; }

public abstract class Event : BaseEntity
    public int PersonID { get; set; }

    public string EventName { get; set; }

    public virtual Person Person { get; set; }

public class DerivedEvent1 : Event
    public bool IsDerivedEvent1 { get; set; }

public class DerivedEvent2 : Event
    public bool IsDerivedEvent2 { get; set; }


If using[NotMappedAttribute] , theState The migration is successful and the property is appropriately ignored for all kinds, however this also removes the property from the OData API, which is something we don't want.

as we need theState We are not using the property inside the OData API.[NotMappedAttribute] the fluid configuration, though.

Fluent arrangement:

modelBuilder.Types<BaseEntity>().Configure(clazz => clazz.Ignore(prop => prop.State));

add-migration Initial -Force

leads to this error

You cannot use Ignore method on the property 'State' on type 'EntityFrameworkIgnoreProperty.Models.DerivedEvent1' because this type inherits from the type 'EntityFrameworkIgnoreProperty.Models.BaseEntity' where this property is mapped. To exclude this property from your model, use NotMappedAttribute or Ignore method on the base type.

The Fluent api is what I need to make this work with, and I need to do this for all derived kinds ofBaseEntity at once.

I can't accomplish this by hand for every single entity in my actual project, which has more than 100 entities, particularly in light of potential future growth.

8/17/2017 9:30:49 AM

Accepted Answer

The issue seems to be connected to the following:Types Every class inheriting from another calls the method body.BaseEntity EF inheritance problems are being brought on by this, either directly or indirectly.

You may use a filter to only apply the setting to direct derived types, such as these:

    .Where(t => t.BaseType == typeof(BaseEntity))
    .Configure(clazz => clazz.Ignore(prop => prop.State));
8/17/2017 9:38:15 AM

Related Questions


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