OData не распознает свойства моей коллекции - PullRequest
5 голосов
/ 13 мая 2019

Когда я публикую ShakeoutDocument без заполненных коллекций, OData Serializer понимает JSON и правильно заполняет ODataActionParameters.

Однако, когда я добавляю дочернюю запись в одно из свойств коллекции ShakeoutDocument's ... параметр Odata Controller's ODataActionParameters равен нулю. Я сузил его до OData EDM Model Configuration.

  • Как правильно "охарактеризовать" свойства коллекции (ниже) для конфигурации модели EDM

В КЛАССЕ СМОТРИТ Коллекции - это детали и печати (ниже)

public class ShakeoutDocument : Document
{
    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?
}

Моя текущая конфигурация EDM выглядит так:
Я пробовал различные подходы, как определено ЗДЕСЬ . Странно то, что ... IObjectState очень хорошо регистрируется.

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;
}

ОТПРАВКА ЭТОГО РАБОТЫ JSON:
Если я SEND, то это JSON БЕЗ заполнения коллекций Details & Seals .. оно сериализуется правильно.

Если я заполняю коллекции Details & Seals ... параметр равен NULL.

{
    "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"
            }
        ]
    }
}

ПОЛУЧЕНИЕ НЕУДАЧИ:
Это показывает объект, отправленный НАЗАД К Клиенту ОТ Сервиса и JSON, полученный Клиентом.

enter image description here

UNBOUND FUNCTION выглядит так:
Для полноты я включаю это ...

[HttpPost]
[ODataRoute("SaveShakeoutDocument")]
public IHttpActionResult SaveDocument(ODataActionParameters parameters)
{
    var provider = Application.ShakeoutDocumentProvider;
    var document = null as ShakeoutDocument;

    try
    {
        document = provider.Get(key);
    }
    catch (Exception ex)
    {
        LogException(ex);

        throw;
    }

    return Ok(document);
}

ОБНОВЛЕНИЕ 1:
$ MetaData, кажется, понимает типы коллекций ...

<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>

ОБНОВЛЕНИЕ 2:
Я только что заметил ... ShakeoutDetails & ShakeoutDetail имеют привязку NavigationPropertyBeter "Meter" ... который отсутствует в 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>

1 Ответ

0 голосов
/ 22 мая 2019

В соответствии с вашим последним обновлением я считаю интересной проблему с привязкой свойства навигации.

Метр не разрешает, вы должны посмотреть, что https://odata.github.io/WebApi/13-03-NavigationPropertyBindingWithComplexType/ (Также посмотрите переходные действия, если вам нужно)

...