WebApi icon indicating copy to clipboard operation
WebApi copied to clipboard

OData Doesn't Recognize My Collection Properties

Open PrisonerZERO opened this issue 7 years ago • 1 comments

I am very new to OData...

When I POST ShakeoutDocument without the collections populated, the OData Serializer understands the JSON & populates the ODataActionParameters correctly.

However, when I add a child-record into either of the ShakeoutDocument's collection properties...the Odata Controller's ODataActionParameters parameter is null. I have narrowed it down to the OData EDM Model Configuration.

  • How do I correctly "characterize" the collection properties (below) to the EDM Model Configuration

THE CLASS LOOKS LIKE: The collections are the Details & Seals (below)...

public class ShakeoutDocument: Document, IDocument, IStateful //<-- Document is an Entity...ShakeoutDocument is NOT
{
	public IObjectState ObjectState { get; set; } //<-- This "registers" just fine?

	public int ShakeoutId { get; set; }

	public string SchedulingBatch { get; set; }

	public int? ProductId { get; set; }

	public decimal? Gravity { get; set; }

	public decimal? Temperature { get; set; }

	public decimal? SedimentAndWater { get; set; }

	public DateTime? BatchEndDate { get; set; }

	public DateTime? SampleWorkedDate { get; set; }

	public string Witness { get; set; }

	public string Notes { get; set; }

	public List<ShakeoutDetail> Details { get; set; } //<-- How do I "register" this?

	public List<ShakeoutSeal> Seals { get; set; } //<-- How do I "register" this?
}

MY CURRENT EDM CONFIGURATION LOOKS LIKE: I have tried various approaches as defined [HERE][1]. The odd thing is...IObjectState seems to registers just fine.

private static  IEdmModel GetEdmModel()
{
	var modelBuilder = new ODataConventionModelBuilder();
	modelBuilder.Namespace = "Pulse";
	modelBuilder.ContainerName = "PulseContainer";

	// -------------------
	// ENTITY SETS: Normal
	modelBuilder.EntitySet<tblDbcCommunications>("Communication");
	modelBuilder.EntitySet<tblDbcItems>("Item");
	modelBuilder.EntitySet<Meter>("Meter");
	modelBuilder.EntitySet<Product>("Products");
	modelBuilder.EntitySet<WarehouseMeter>("WarehouseMeters");
	modelBuilder.EntitySet<tblDbcCircuitOwner>("tblDbcCircuitOwner");
	modelBuilder.EntitySet<ShakeoutDetail>("ShakeoutDetails");
	modelBuilder.EntitySet<ShakeoutSeal>("ShakeoutSeals");

	// --------------------
	// ENTITY SETS: Complicated
	ConfigureEntitySet(modelBuilder, modelBuilder.EntitySet<ShakeoutDocument>("ShakeoutDocuments"));

	// ------------
	// KEY BINDINGS: Non-Entity
	//
	// - This area is for DTO's (there is no EF Configuration for these)
	modelBuilder.EntityType<WarehouseMeter>().HasKey(x => x.METER_ID);

	// -----------------
	// UNBOUND FUNCTIONS
	//  - Action    = Post
	//  - Function  = Get

	//----
	// ShakeoutDocument
	var createShakeoutDocument = modelBuilder.Action("CreateShakeoutDocument").ReturnsFromEntitySet<ShakeoutDocument>("ShakeoutDocument");
	createShakeoutDocument.CollectionParameter<int>("identities");

	var deleteShakeoutDocument = modelBuilder.Action("DeleteShakeoutDocument").ReturnsFromEntitySet<ShakeoutDocument>("ShakeoutDocument");
	deleteShakeoutDocument.Parameter<int>("key");

	var getShakeoutDocument = modelBuilder.Function("GetShakeoutDocument").ReturnsFromEntitySet<ShakeoutDocument>("ShakeoutDocument");
	getShakeoutDocument.Parameter<int>("key");

	var saveShakeoutDocument = modelBuilder.Action("SaveShakeoutDocument").ReturnsFromEntitySet<ShakeoutDocument>("ShakeoutDocument");
	saveShakeoutDocument.Parameter<ShakeoutDocument>("document");

	//----
	// ShakeoutDetail
	var saveShakeoutDetail = modelBuilder.Action("SaveShakeoutDetail").ReturnsFromEntitySet<ShakeoutDetail>("ShakeoutDetail");
	saveShakeoutDetail.Parameter<ShakeoutDetail>("detail");

	//----
	// ShakeoutSeal
	var saveShakeoutSeal = modelBuilder.Action("SaveShakeoutSeal").ReturnsFromEntitySet<ShakeoutSeal>("ShakeoutSeal");
	saveShakeoutSeal.Parameter<ShakeoutSeal>("seal");

	//----
	// Product
	var listProduct = modelBuilder.Function("ListProducts").ReturnsCollectionFromEntitySet<Product>("Product");

	//----
	// WarehouseMeter
	var findPulseMeter = modelBuilder.Function("FindWarehouseMeter").ReturnsCollectionFromEntitySet<WarehouseMeter>("WarehouseMeter");
	findPulseMeter.Parameter<string>("search");

	return modelBuilder.GetEdmModel();
}

private static EntitySetConfiguration<ShakeoutDocument> ConfigureEntitySet(ODataConventionModelBuilder modelBuilder, EntitySetConfiguration<ShakeoutDocument> configuration)
{
	// ---------------------
	// Collection Properties

	// FAILS: Meaning, the collection is not serialized & returned
	//var details = configuration.EntityType.CollectionProperty(x => x.Details);
	//var seals = configuration.EntityType.CollectionProperty(x => x.Seals);

	// FAILS: Meaning, the collection is not serialized & returned
	//configuration.HasManyBinding(x => x.Details, "ShakeoutDetail");
	//configuration.HasManyBinding(x => x.Seals, "ShakeoutSeal");

	// ------------------
	// Complex Properties
	configuration.EntityType.ComplexProperty(x => x.ObjectState);

	// IObjectState
	var objectState = modelBuilder.ComplexType<IObjectState>();
	objectState.CollectionProperty(x => x.Events);

	// Object State
	var newObjectState = modelBuilder.ComplexType<New>();
	newObjectState.DerivesFrom<IObjectState>();

	var submittedObjectState = modelBuilder.ComplexType<Submitted>();
	submittedObjectState.DerivesFrom<IObjectState>();

	// Object State Event
	var isNewObjectStateEvent = modelBuilder.ComplexType<IsNew>();
	isNewObjectStateEvent.DerivesFrom<IObjectStateEvent>();

	var isSubmittedObjectStateEvent = modelBuilder.ComplexType<IsSubmitted>();
	isSubmittedObjectStateEvent.DerivesFrom<IObjectStateEvent>();

	return configuration;
}

RETURNED JSON: Details & Seals collections ARE ALWAYS EMPTY...but IObjectState returns just fine?

{
	"DocumentTypeId": 1,
	"GlobalId": "e8c9d71d-2773-e911-b71a-8cdcd4471a95",
	"ParentId": null,
	"AuthorId": 1,
	"PublisherId": null,
	"RevisionNumber": 0,
	"PublishedDate": null,
	"IsActive": true,
	"Id": 44,
	"CreateUserId": "(removed)",
	"CreateDate": "2019-05-10T08:25:46.31-05:00",
	"UpdateUserId": "(removed)",
	"UpdateDate": "2019-05-10T08:25:46.31-05:00",
	"ShakeoutId": 44,
	"SchedulingBatch": null,
	"ProductId": null,
	"Gravity": null,
	"Temperature": null,
	"SedimentAndWater": null,
	"BatchEndDate": null,
	"SampleWorkedDate": null,
	"Witness": null,
	"Notes": null,
	"Seals": [],
	"Details": [],
	"ObjectState": {
		"@odata.type": "#Namespace...ShakeoutDocument.New",
		"Name": "New",
		"Events": [
			{
				"@odata.type": "#Namespace...ShakeoutDocument.IsNew",
				"Name": "IsNew"
			}
		]
	}
}

THE UNBOUND FUNCTION LOOKS LIKE: For completeness, I am including this...

[HttpPost]
[ODataRoute("SaveShakeoutDocument")]
public IHttpActionResult SaveDocument(ODataActionParameters parameters)
{
	// Doing awesome stuff here
}

METADATA: The $MetaData seems to understand the collection types...

<EntityType Name="ShakeoutDocument" BaseType="Pulse.Document">
	<Property Name="ObjectState" Type="Pulse.IObjectState" Nullable="false"/>
	<Property Name="ShakeoutId" Type="Edm.Int32" Nullable="false"/>
	<Property Name="SchedulingBatch" Type="Edm.String"/>
	<Property Name="ProductId" Type="Edm.Int32"/>
	<Property Name="Gravity" Type="Edm.Decimal"/>
	<Property Name="Temperature" Type="Edm.Decimal"/>
	<Property Name="SedimentAndWater" Type="Edm.Decimal"/>
	<Property Name="BatchEndDate" Type="Edm.DateTimeOffset"/>
	<Property Name="SampleWorkedDate" Type="Edm.DateTimeOffset"/>
	<Property Name="Witness" Type="Edm.String"/>
	<Property Name="Notes" Type="Edm.String"/>
	<NavigationProperty Name="Details" Type="Collection(Pulse.ShakeoutDetail)"/>
	<NavigationProperty Name="Seals" Type="Collection(Pulse.ShakeoutSeal)"/>
</EntityType>

UPDATE: I just noticed...ShakeoutDetails & ShakeoutDetail have a NavigationPropertyBinding of "Meter"...which is not present in ShakeoutDocument.

<?xml version="1.0" encoding="UTF-8"?>
<EntityContainer Name="PulseContainer">
   <EntitySet Name="Communication" EntityType="Pulse.tblDbcCommunications" />
   <EntitySet Name="Item" EntityType="Pulse.tblDbcItems" />
   <EntitySet Name="Meter" EntityType="Pulse.Meter" />
   <EntitySet Name="Products" EntityType="Pulse.Product" />
   <EntitySet Name="WarehouseMeters" EntityType="Pulse.WarehouseMeter" />
   <EntitySet Name="tblDbcCircuitOwner" EntityType="Pulse.tblDbcCircuitOwner" />
   <EntitySet Name="ShakeoutDetails" EntityType="Pulse.ShakeoutDetail">
      <NavigationPropertyBinding Path="Meter" Target="Meter" />
   </EntitySet>
   <EntitySet Name="ShakeoutSeals" EntityType="Pulse.ShakeoutSeal" />
   <EntitySet Name="ShakeoutDocuments" EntityType="Pulse.ShakeoutDocument" />
   <EntitySet Name="ShakeoutDocument" EntityType="Pulse.ShakeoutDocument" />
   <EntitySet Name="ShakeoutDetail" EntityType="Pulse.ShakeoutDetail">
      <NavigationPropertyBinding Path="Meter" Target="Meter" />
   </EntitySet>
   <EntitySet Name="ShakeoutSeal" EntityType="Pulse.ShakeoutSeal" />
   <EntitySet Name="Product" EntityType="Pulse.Product" />
   <EntitySet Name="WarehouseMeter" EntityType="Pulse.WarehouseMeter" />
   <ActionImport Name="CreateShakeoutDocument" Action="Pulse.CreateShakeoutDocument" EntitySet="ShakeoutDocument" />
   <ActionImport Name="DeleteShakeoutDocument" Action="Pulse.DeleteShakeoutDocument" EntitySet="ShakeoutDocument" />
   <FunctionImport Name="GetShakeoutDocument" Function="Pulse.GetShakeoutDocument" EntitySet="ShakeoutDocument" IncludeInServiceDocument="true" />
   <ActionImport Name="SaveShakeoutDocument" Action="Pulse.SaveShakeoutDocument" EntitySet="ShakeoutDocument" />
   <ActionImport Name="SaveShakeoutDetail" Action="Pulse.SaveShakeoutDetail" EntitySet="ShakeoutDetail" />
   <ActionImport Name="SaveShakeoutSeal" Action="Pulse.SaveShakeoutSeal" EntitySet="ShakeoutSeal" />
   <FunctionImport Name="ListProducts" Function="Pulse.ListProducts" EntitySet="Product" IncludeInServiceDocument="true" />
   <FunctionImport Name="FindWarehouseMeter" Function="Pulse.FindWarehouseMeter" EntitySet="WarehouseMeter" IncludeInServiceDocument="true" />
</EntityContainer>

PrisonerZERO avatar May 10 '19 17:05 PrisonerZERO

Hi, do you still have this problem? Can you test if the same thing happens when you remove all your DerivesFrom's?

I'm currently having an issue where my expands don't work on collections in the basetype after adding the DerivesFrom.

RobbeSchepens avatar Feb 12 '20 12:02 RobbeSchepens