Search code examples
c#odataasp.net-web-api-odata

ODataConventionModelBuilder sees my List property as a new EntitySet and throws 'The entity 'List_1Of[Type]' does not have a key defined.'


I apologize if this is very trivial, I am new to OData and I am trying to set up my model.

I have a class like this:

public class EventInfo
{
    public bool Open{ get; set; }
    public List<Attendee> Attendees { get; set; }
    public string Author { get; set; }
    [Required]
    public string Description { get; set; }
    public DateTime Start { get; set; }
    public DateTime End { get; set; }
    public int Id { get; set; }
    public string Image{ get; set; }
    [Required]
    public string Title { get; set; }
    public string Address{ get; set; }
}

So I added this to my ODataConventionModelBuilder :

builder.EntitySet<EventInfo>("EventInfos");
builder.EntityType<EventInfo>().HasKey(x => x.Id);
builder.EntityType<EventInfo>().HasOptional(x => x.Attendees);

But when I tried to run it, I got this error:

The entity 'List_1OfAttendee' does not have a key defined.

Apparently the ODataConventionModelBuilder is trying to turn my List<Attendee> into a new entity requiring an ID, but this is just a complex type that I use here as part of the model, it's not a navigation property and it is not used anywhere else.

So I tried adding Attendee to my model like this:

builder.EntitySet<Attendee>("Attendees");
builder.EntityType<Attendee>().HasKey(x => x.Name);

But I got the exact same message, so I tried to change the EntitySet name to match the name on the error:

builder.EntitySet<Attendee>("List_1OfAttendee");
builder.EntityType<Attendee>().HasKey(x => x.Name);

But again it didn't change anything.

For reference my backend is RavenDB, and I don't use Entity Framework.

I am really confused, the only examples I found are using a collection of complex types for navigation properties only, but here it's just a simple property necessary for my model. Any advice is welcome


Solution

  • If your Attendee class shouldn't have its own entity set, you can define that it is a complex type like so:

    builder.ComplexType<Attendee>("Attendees");