AspNetCoreOData icon indicating copy to clipboard operation
AspNetCoreOData copied to clipboard

System.InvalidCastException: Unable to cast object of type 'Microsoft.OData.UnknownEntitySet' to type 'Microsoft.OData.Edm.IEdmEntitySet'.

Open cxc256 opened this issue 3 years ago • 3 comments

I built a post endpoint. The data gets inserted into the database fine. But it throws the above error when returning: System.InvalidCastException: Unable to cast object of type 'Microsoft.OData.UnknownEntitySet' to type 'Microsoft.OData.Edm.IEdmEntitySet'.

However, doing a Get returns the object fine.

I'm using .Net 5.0. Microsoft.AspNetCore.OData 8.0.4

[HttpPost( "Persons({key})/PersonalAttributes" )]
public IActionResult CreatePersonalAttributeForPerson( int key , [FromBody] PersonAttribute personAttribute )
{
	try
	{
		if( !ModelState.IsValid )
		{
			logModelStateError( );
			return BadRequest( ModelState );
		}

		//Insert personAttribute into database

		return Created( personAttribute );
	}
	catch( Exception ex )
	{
		_logger.LogError( ex , "{PersonId} {@PersonAttribute}" , key , personAttribute );
		return StatusCode( StatusCodes.Status500InternalServerError );
	}
}

cxc256 avatar Dec 19 '21 22:12 cxc256

Hi @cxc256, can you please share your CSDL for this? If personAttribute is not a contained navigation property, then maybe the issue is that there is a missing navigation property binding in PersonalAttributes?

corranrogue9 avatar Dec 21 '21 17:12 corranrogue9

CSDL.txt

cxc256 avatar Jan 13 '22 23:01 cxc256

Hey @cxc256 I have looked at the CSDL. You dont have navigation property binding for PersonalAttributes. You should have something similar to this in your csdl.

<EntityContainer Name="Container">
    <EntitySet Name="Persons" EntityType="Namespace.Person">
        <NavigationPropertyBinding Path="PersonalAttributes" Target="Persons"/>
...
...
    <EntitySet />
<EntityContainer  />

Alternatively PersonAttribute should be contained within Person entity.

KenitoInc avatar Apr 14 '22 08:04 KenitoInc

Cause of the issue pointed out

gathogojr avatar Nov 01 '23 07:11 gathogojr