'Support OData for an entity with 2 composite keys in ASPNetBoilerplate
I'm using ASP.NET Boilerplate framework for ASP.NET Core. How do I add a OData controller to support an entity with 2 composite keys.
I have the composite key defined as:
modelBuilder.Entity<vFamilyItem>().HasKey(t => new { t.Id, t.FamilyId });
I have my get function defined as follows:
public SingleResult<vFamilyItem> Get([FromODataUri] int keyId, [FromODataUri] int keyFamilyId)
{
CheckGetPermission();
var entity = Repository.GetAll().Where(e => e.Id.Equals(keyId) && e.FamilyId.Equals(keyFamilyId));
return SingleResult.Create(entity);
}
but get a 404 error when calling http://localhost:21021/odata/vFamilyItems(Id=77623,FamilyId=2648)
Solution 1:[1]
I think the issue is the order of the 2 keys in your API call.
From what I can tell, the key names and values need to be alphabetical in your call:
http://localhost:21021/odata/vFamilyItems(FamilyId=2648,Id=77623)
It doesn't seem to matter what order they are in the model, the FluentAPI defining its key nor even the C# function signature.
The way I figured out I had my 2 the wrong way around compared to expected was to hit the end point:
http://localhost:21021/$odata
which requires the UseODataRouteDebug() call in startup.cs:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseODataRouteDebug();
...
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Chris |