I am using Web API 2.2 project with OData4 package + Entity Framework 6.
I have a User
class which inherits from People
class. People
class itself inherits from Person
class.
[Table("People")]
public class People : Person
{ }
public class User: People
{
[Key]
public int UserId { get; set; }
[ForeignKey("Anonymous")]
public int AnonymousId { get; set; }
}
When I execute the code an exception is thrown in WebApiConfig::Register
method on this line
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: null,
model: builder.GetEdmModel());
Error:
An exception of type 'System.InvalidOperationException' occurred in System.Web.OData.dll but was not handled in user code
Additional information: Cannot define keys on type 'User' deriving from 'People'. Only the root type in the entity inheritance hierarchy can contain keys.
I have tried solution given at http://stack247.wordpress.com/2013/02/28/cannot-define-keys-on-type-deriving-from-only-root-type-in-entity-inheritance-hierarchy-can-contain-keys/
but it did not work.
I tried :
builder.EntityType<User>().DerivesFrom<People>();
builder.EntityType<People>().DerivesFrom<Element>();
As suggested by Gert we have removed key attribute from child class.
This has resolved the problem.