3
votes

I'm using Asp.Net WebApi Odata V4 with Entity Framework 6. I'm trying to expand navigation property of a derived class, but im getting below error.

Base Entity

public abstract class BaseEntity
{
    [ForeignKey("CreatedUser")]
    public string CreatedBy { get; set; }

    public virtual User CreatedUser { get; set; }
}

Entity

public class Book: BaseEntity
{
    public int BookId {get;set;}
}

Odata Model Builder

builder.EntityType<BaseEntity>();
builder.EntitySet<Book>("Books");

Odata Query

http://localhost/svc/Books(1)?$expand=CreatedUser

Error:

The 'TypeAs' expression with an input of type 'App.Models.Book' and a check of type 'App.Models.BaseEntity' is not supported. Only entity types and complex types are supported in LINQ to Entities queries.

How can I overcome this issue?

1
This question looks a bit similar to this. Can you find an answer there?GWigWam
I think you shouldn't register BaseEntity.Gert Arnold
@GertArnold No, Same error occurs even if i didn't register BaseEntityRahul
@GWigWam Tried that suggestions, but it does not work for me.Rahul

1 Answers

0
votes

For your model builder, do not register BaseEntity as an EntityType, instead use the concrete type:

builder.EntityType<Book>();
builder.EntitySet<Book>("Books");