A couple of issues with EF 4.2 and inherited class association properties

495 Views Asked by At

I have a couple of issues with EF 4.2 and the fluent api with Code First design approach and base class property inheritance (Table Per Concrete Type)...

1) Fluent Api to ignore base class property (non-primitive property) causes EF error.

It appears that my ignore statements in the fluent api are not being processed properly on my base class property.

class BaseContentElement {

public virtual BaseContentElemnt Parent {get; set;}

public int Id {get;set;}
...
}

class ChapterElement : BaseContentElement {

public virtual CourseElement Course { 
    get { return base.Parent as CourseElement; } set {base.Parent = value; } 
}

...

}

In fluent api, the statement

var config = new EntityTypeConfiguration<ChapterElement>();

config.Map(m =>
{
    m.MapInheritedProperties();
    m.ToTable("Chapter", Schema);
});

config.HasKet( ch => ch.Id );
config.Ignore( ch => ch.Parent );

causes EF to generate the error that 'Id' is not found on the class ChapterElement...

If however, I add the data annotation [NotMapped] on the Parent property in class BaseContentElement, EF engine is appy and the DB is created...

2) Base class association property to a lookup table causes errors in duplicate key insertion...

public abstract class BaseListItemElement : BaseContentElement, IComparable
{
    public int Id { get; set; }
    public int Index { get; set; }
    public virtual StaticContentBlockElement Item { get; set; }
    public virtual eAnimationDirection AnimeDirectionEnum
    {
        get
        {
            if (AnimeDirection != null)
            {
                return AnimeDirection.EnumValue;
            }

            return eAnimationDirection.None;
        }
        set
        {
            AnimeDirection = AnimationDirection.Lookup[value];
        }
    }
    public virtual AnimationDirection AnimeDirection { get; set; }

...

public class TextListItem : BaseListItemElement {
...
}

In my app I keep a lookup table in memory of all the lookup table values (the app works with the enum eAnimationDirection and the DB is updated with the AnimeDirection property). Before I add the records to the DBContext, I attach the lookup values to the context DbContext.AnimationDirections.Attach(... for each item in lookup table ...).

When I add the entities to the DB I get a duplicate insertion error on the AnimationDirection table (which is a lookup table)..

If however I move the properties, AnimeDIrection and AnimeDirectionEnum to the sub-class, TextListItemElement, the operations succeed without error... I have 3 other sub-classes which also share the properties (actually there are two lookup properties on the base class that I share, so it would not be advisable to move the properties to the sub-classes)...

Either this is a bug in EF 4.2 or it may be due to my base class being abstract (Microsoft has a bad history with base abstract classes....)

1

There are 1 best solutions below

2
On

Those two questions are completely unrelated so they should not be asked together. Second questions should be asked separately together with code sample showing how do you attach / add entities because that significant part of the problem is missing and your description really doesn't make it clear - the real reproducible code does.

Answer to the first question: It is not possible. Once any part of base class is mapped you cannot ignore it in derived class. Derived class must contain all properties mapped in the base class.