0

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

AlineF
  • 3
  • 3

2 Answers2

0

By writing HasOptional you indicate to ODATA that this is a navigation property, which requires a key. If you do not have a key, but merely use the Attendees as a plain list as you do with Tags, you should not define a navigation property.

When I define such a construct I normally do it like this:

// main model
public class Model
{
    public long Id { get; set; }

    public virtual ICollection<NameValuePair> NameValuePairs { get; set; }

    // other properties ...
}

// complex type
public class NameValuePair
{
    public string Name { get; set; }

    public string Value { get; set; }

    // other properties ...
}

// builder
builder.EntitySet<Model>("Models");
builder.EntitySet<NameValuePair>("NameValuePairs");
Ronald Rink 'd-fens'
  • 1,289
  • 1
  • 10
  • 27
0

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");
juunas
  • 54,244
  • 13
  • 113
  • 149
  • Thank you, that did the trick ! Can't believe I didn't spot that in the documentation ... They mainly talk about the attributes on the Convention model builder section – AlineF Mar 14 '17 at 14:58