c# - Is it possible to have multiple controllers for a single entity type? -


i have entity framework model. let's have object foo , object bar. related, foo has navigation property bar, , vice versa.

now, odata endpoint, i'd have 2 collections available foo, instance declared so:

var builder = new odataconventionmodelbuilder { namespace = "test" }; builder.entityset<foo>("fools"); builder.entityset<foo>("footballs"); builder.entityset<bar>("bars"); 

the idea here access fools go through foolscontroller , access footballs go through footballscontroller, return different sets of data in each endpoint.

trying this, however, causes following notsupportedexception error message:

cannot automatically bind navigation property 'foothing' on entity type 'foo' entity set or singleton 'bars' because there 2 or more matching target entity sets or singletons. matching entity sets or singletons are: fools, footballs.

i sort of understand issue, if know fact footballs have bars, there way me system understand bars have footballs?

the answer absolutely yes. there many fluent apis can call set binding manually, suppress convention binding. example:

hasmanybinding hasrequiredbinding hasoptionalbinding hassingletonbinding ... 

based on information, can call following make binding manually:

builder.entityset<bar>("bars").hasrequiredbinding(b => b.foothing, "fools"); 

i create simple foo , bar class model test. below result shows metadata:

<?xml version="1.0" encoding="utf-8"?> <edmx:edmx version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">   <edmx:dataservices>     <schema namespace="webapitest" xmlns="http://docs.oasis-open.org/odata/ns/ed m">       <entitytype name="foo">         <key>           <propertyref name="id" />         </key>         <property name="id" type="edm.int32" nullable="false" />       </entitytype>       <entitytype name="bar">         <key>           <propertyref name="id" />         </key>         <property name="id" type="edm.int32" nullable="false" />         <navigationproperty name="foothing" type="webapitest.foo" nullable="fals e" />       </entitytype>     </schema>     <schema namespace="test" xmlns="http://docs.oasis-open.org/odata/ns/edm">       <entitycontainer name="container">         <entityset name="fools" entitytype="webapitest.foo" />         <entityset name="footballs" entitytype="webapitest.foo" />         <entityset name="bars" entitytype="webapitest.bar">           <navigationpropertybinding path="foothing" target="fools" />         </entityset>       </entitycontainer>     </schema>   </edmx:dataservices> </edmx:edmx> 

as can see, "hasrequiredbinding" can make navigation property non-nullable, while, "hasoptionbinding" can make nullable.

hope can help. thanks.


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -