Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Odata Bind uses EntityType not EntitySet #511

Open
jamesstanleystewart opened this issue Aug 2, 2018 · 6 comments
Open

Odata Bind uses EntityType not EntitySet #511

jamesstanleystewart opened this issue Aug 2, 2018 · 6 comments
Assignees
Labels
Milestone

Comments

@jamesstanleystewart
Copy link

jamesstanleystewart commented Aug 2, 2018

Possibly just a question, possibly a bug.

Given some simplified metadata:

<EntityType Name="XResource">
	<Key>
		<PropertyRef Name="Id"/>
	</Key>
	<Property Name="Id" Type="Edm.Guid" Nullable="false"/>
	<NavigationProperty Name="Y" Type="Models.YResource"/>
</EntityType>

<EntitySet Name="Xs" EntityType="Models.XResource">

<EntityType Name="YResource">
	<Key>
		<PropertyRef Name="Id"/>
	</Key>
	<Property Name="Id" Type="Edm.Guid" Nullable="false"/>
</EntityType>

<EntitySet Name="Ys" EntityType="Models.YResource">

Odata Service endpoints:

service/v2/Xs(Guid) returns a XResource
service/v2/Ys(Guid) returns a YResource

Attempting to create an XResource entry:

Client.For<OdataServiceX>("Xs")
	.Set(backendX)
	.InsertEntryAsync()

where C#:

backendX = BackendX { y = BackendY(Guid) }

generates odata body:

{
        @odata.type=#Models.XResource 
	[email protected]=service/v2/YResource(Guid)
}

where I would expect

{
        @odata.type=#Models.XResource 
	[email protected]=service/v2/Ys(Guid)
}

Changing Type name of "BackendY" to "Y" will make the client correctly find service/v2/Ys(Guid)

It seems the client uses the EntityType and not the EntitySet for trying to best match an unknown type name?
Is there a way to use classes that are not named the same as the model endpoint names?

@object
Copy link
Member

object commented Aug 2, 2018

I agree that EntitySet name should be preferred to EntityType, I will check this out. As a workaround you can attach either Table or DataContract attribute to a class, e.g.

[Table(Name="Ys")]
public class OdataServiceY

@object object self-assigned this Aug 2, 2018
@object object added the bug label Aug 2, 2018
@jamesstanleystewart
Copy link
Author

The annotations didn't seem to work for me. I tried both Table and DataContract on the class and Column on the property.

What did work, is pulling the source into my project and adding:

if (TryGetEntityType(typeName, out entityType) && TryGetEntitySetByType(entityType.FullTypeName(), out entitySet))
{
    return entitySet.Name;
}

To metadata.cs just before the entityType.Name is used (line 124).

private bool TryGetEntitySetByType(string entityType, out IEdmEntitySet entitySet) {
    if (entityType.Contains("/"))
	    entityType = entityType.Split('/').First();

    entitySet = _model.SchemaElements
	    .Where(x => x.SchemaElementKind == EdmSchemaElementKind.EntityContainer)
	    .SelectMany(x => (x as IEdmEntityContainer).EntitySets())
	    .BestMatch(x => x.Type.AsElementType().FullTypeName(), entityType, _session.Settings.NameMatchResolver);

    return entitySet != null;
}

Does this solution break logic elsewhere?

@object
Copy link
Member

object commented Aug 12, 2018

Thank you for your finding. I will investigate this further.

@object object added this to the 5.5 milestone Aug 12, 2018
@object object modified the milestones: 5.5, 5.10 Sep 4, 2019
@JaxomF
Copy link

JaxomF commented May 15, 2020

Hi,
Any update on this bug ?

@CJTalbot87
Copy link

I have also have this issue where the EntityType is used and not the EntitySet, the DataAnnotations also did not work for me.

Is there any "clean" way to override the used OData Bind entity name?

@swidz
Copy link

swidz commented Nov 30, 2023

I am having also this issue, I have not digged into the source code yet, but it behaves rather strange. For most of the odata.bindings the entitySet name is used but for the one which actually causes the problem an entity type name is used.

eitsoupii pushed a commit to eitsoupii/Simple.OData.Client that referenced this issue Jan 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants