Search code examples
c#entity-framework-6odataodata-v4

Add to odata collection property in .NET


Using the odata-v4 service connection in visual studio C# .NET, I get an entity of type testDefinition. testDefinition has property called features which is a collection of entities of type feature.

In the DB, testDefinition to feature is many to many with a junction table.

enter image description here

In my code, I add a service reference to a web service serving the EDMX of the DB.

Code gets generated correctly and I run:

var dsc = new Container(new Uri("http://webserver/webapi/odata/"));

var someFeature = new Feature
{
    name = $"Sample feature created with C# {DateTime.UtcNow}",
};

var someOtherFeature = new Feature
{
    name = $"Sample other feature created with C# {DateTime.UtcNow}",
};

dsc.AddToFeature(someFeature);
dsc.AddToFeature(someOtherFeature);

dsc.SaveChanges();

var someTestDefinition = new TestDefinition
{
    name = $"Sample test created with C# {DateTime.UtcNow}",
    description = $"A nice succinct description",
};    

dsc.AddToTestDefinition(someTestDefinition);
dsc.SaveChanges();

someTestDefinition.features.Add(someFeature);
someTestDefinition.features.Add(someOtherFeature);

dsc.SaveChanges();

The problem is the mapping from the test definition to features is not recorded in the database.

Has anyone encountered this issue, or better yet, resolved it?

For Reference:

enter image description here


Solution

  • I can't say when this was implemented but it works in later versions, the earliest verison I can confirm this against is OData.Client v 7.6.2

    If this is an issue for any new users on current versions of the OData Client and Server libraries it generally indicates that the schema is not correctly configured or published.

    Also check that the API uses a similarly current OData runtime, the above test was performed against API with Microsoft.AspNet.OData v7.3.0