0

I have the following class setup

 public abstract class SearchElement
{
    public int Id { get; set; }

    public SearchElement parent { get; set; }

    public int Order { get; set; }

    public UserQuery UserQuery { get; set; }
}

public class SearchGroup : SearchElement
{
    public virtual ICollection<SearchElement> SearchObjects { get; set; }

    public bool IsAndOperator { get; set; }

    public SearchGroup()
    {
        this.SearchObjects = new List<SearchElement>();
    }
}
public abstract class SearchCondition<IContext, OutputType> : SearchElement
{
    public ComparisonTypes Comparison { get; set; }

    public string Value { get; set; }

    public abstract Expression<Func<OutputType, bool>> BuildConditionQuery(IContext context);
}
public class SearchPackage : SearchCondition<ISearchContext, ProjectParticipantQuestionnaireResponseGroup>
{
    public override System.Linq.Expressions.Expression<Func<ProjectParticipantQuestionnaireResponseGroup, bool>> BuildConditionQuery(ISearchContext context)
    {
        return this.BuildCondition<ProjectParticipantQuestionnaireResponseGroup, int>(r => r.Package.Id, int.Parse(this.Value), this.Comparison);            
    }
}

Now for some reason, when in the EntityFramework context I specify:

public DbSet<SearchElement> SearchElements { get; set; }

The SearchGroup class gets detected and the appropriate fields get created in the SearchElement table. However, the SearchPackage class does not get detected and it's fields are not created in the SearchElement table.

I can of course create a DbSet for the SearchPackage, but there are multiple similar classes (same inheritance, although some with difference values) and I don't want to create a DbSet for each of them. Does anyone has suggestions about what I can do?

For clarity: I am using Entity Framework 6.1.3 and C# 4.5.1

1 Answer 1

1

EF 6 cannot map a CLR generic type. (Sorry, I can't find an authoritative reference on this right now.) This is the problem, not one of inheritance generally. When EF traverses your inheritance "tree," it gets to SearchCondition<,> and gives up.

Finding a way around this will probably require a rethinking of your object model to something more serialization-friendly. Is there a way that you can split your object-model into a set of services (that might contain generics) that interacts with a set of more easily mapped DTOs?

Another (possible, untested) option: Make an ISearchElement mapped interface. SearchPackage should implement it directly. This way, SearchPackage--and other SearchCondition<,> implementors should get "picked up" by EF inheritance traversal.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your response. I could be wrong, but as far as I know EF doesn´t support Interface mapping. Is there a workaround for that that Google doesn't tell me?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.