This question is locked. New answers and comments are not allowed.
Hi,
I'm trying to build an OData v4 service using the Microsoft-provided API (tutorial), with the data services layer provided by Telerik Data Access. I have moderately complex data model, which includes a few different cases of inheritance.
This is registered in my model like this:
The interesting feature here is that I have a derived class with a navigation property with a collection of derived classes.
I have written a simple OData controller to query for Teachers:
Everything works fine - until I attempt to query my OData service with an "$expand" directive:
GET http://localhost/odata/Teachers(1)?$expand=Addresses
This query will result in a NullReferenceException from within the Telerik code:
Object reference not set to an instance of an object.
at OpenAccessRuntime.Relational.fetch.FopGetProjection.init(SelectExp root)
The error doesn't come up if I don't have the TeachingVenues collection defined. The error doesn't come up if I am querying the data context directly. The error doesn't come up if Teacher is the base class.
Is there something I'm missing?
Paul S.
I'm trying to build an OData v4 service using the Microsoft-provided API (tutorial), with the data services layer provided by Telerik Data Access. I have moderately complex data model, which includes a few different cases of inheritance.
public class Person{ public int Id { get; set; } public string Name { get; set; } public IList<Address> Addresses { get; set; }}public class Teacher : Person{ public IList<TeachingVenue> TeachingVenues { get; set; }}public class Address{ public int Id { get; set; } public string AddressText { get; set; } public string City { get; set; } public string Province { get; set; } public string Country { get; set; }}public class TeachingVenue{ public int Id { get; set; } public string Name { get; set; }}public class Lab : TeachingVenue{ public string Facilities { get; set; }}public class Classroom : TeachingVenue{ public string Building { get; set; }}This is registered in my model like this:
protected override IList<MappingConfiguration> PrepareMapping(){ List<MappingConfiguration> mappingConfigurations = new List<MappingConfiguration>(); var people = new MappingConfiguration<Person>(); people.MapType().ToTable("People"); people.HasDiscriminator().ToColumn("Type"); people.HasDiscriminatorValue("people"); people.HasProperty(p => p.Id).IsIdentity(KeyGenerator.Autoinc); people.HasAssociation(p => p.Addresses) .ToColumn("PersonId", "Addresses"); mappingConfigurations.Add(people); var teachers = new MappingConfiguration<Teacher>(); teachers.MapType().Inheritance(InheritanceStrategy.Flat).ToTable("People"); teachers.HasDiscriminatorValue("teacher"); teachers.HasAssociation(p => p.TeachingVenues) .ToColumn("TeacherId", "TeachingVenues"); mappingConfigurations.Add(teachers); var addresses = new MappingConfiguration<Address>(); addresses.MapType().ToTable("Addresses"); addresses.HasProperty(a => a.Id).IsIdentity(KeyGenerator.Autoinc); mappingConfigurations.Add(addresses); var labsorclassrooms = new MappingConfiguration<TeachingVenue>(); labsorclassrooms.MapType().ToTable("TeachingVenues"); labsorclassrooms.HasDiscriminator().ToColumn("Type"); labsorclassrooms.HasProperty(lc => lc.Id).IsIdentity(KeyGenerator.Autoinc); mappingConfigurations.Add(labsorclassrooms); var classrooms = new MappingConfiguration<Classroom>(); classrooms.MapType().Inheritance(InheritanceStrategy.Flat).ToTable("TeachingVenues"); classrooms.HasDiscriminatorValue("class"); mappingConfigurations.Add(classrooms); var labs = new MappingConfiguration<Lab>(); labs.MapType().Inheritance(InheritanceStrategy.Flat).ToTable("TeachingVenues"); labs.HasDiscriminatorValue("lab"); mappingConfigurations.Add(labs); return mappingConfigurations;}The interesting feature here is that I have a derived class with a navigation property with a collection of derived classes.
I have written a simple OData controller to query for Teachers:
public class TeachersController : ODataController{ protected TelerikModel context = new TelerikModel(); [EnableQuery] public SingleResult<Teacher> Get(int key) { var query = context.GetAll<Teacher>().Where(p => p.Id == key); return SingleResult.Create(query); }}Everything works fine - until I attempt to query my OData service with an "$expand" directive:
GET http://localhost/odata/Teachers(1)?$expand=Addresses
This query will result in a NullReferenceException from within the Telerik code:
Object reference not set to an instance of an object.
at OpenAccessRuntime.Relational.fetch.FopGetProjection.init(SelectExp root)
The error doesn't come up if I don't have the TeachingVenues collection defined. The error doesn't come up if I am querying the data context directly. The error doesn't come up if Teacher is the base class.
Is there something I'm missing?
Paul S.