Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF:如何从API POST读取复杂对象?_C#_Json_Entity Framework_Asp.net Web Api - Fatal编程技术网

C# EF:如何从API POST读取复杂对象?

C# EF:如何从API POST读取复杂对象?,c#,json,entity-framework,asp.net-web-api,C#,Json,Entity Framework,Asp.net Web Api,我有以下课程: public partial class ReservationVersion { public ReservationVersion() { this.Reservations = new HashSet<Reservation>(); this.ReservationItems = new HashSet<ReservationItem>(); } public int Reservat

我有以下课程:

public partial class ReservationVersion
{
    public ReservationVersion()
    {
        this.Reservations = new HashSet<Reservation>();
        this.ReservationItems = new HashSet<ReservationItem>();
    }

    public int ReservationVersionID { get; set; }
    public Nullable<int> ReservationID { get; set; }
    public string GUID { get; set; }
    public string StartDate { get; set; }
    public string StartTime { get; set; }
    public string Duration { get; set; }    

    public virtual ICollection<Reservation> Reservations { get; set; }
    public virtual Reservation Reservation { get; set; }
    public virtual ICollection<ReservationItem> ReservationItems { get; set; }

}


public abstract partial class ReservationItem
{
    public ReservationItem()
    {
    }

    public int ReservationItemID { get; set; }
    public Nullable<int> ReservationVersionID { get; set; }
    public string GUID { get; set; }
    public Nullable<int> ParentID { get; set; }
    public Nullable<bool> NeedsConfirmation { get; set; }
    public string ReservationSummary { get; set; }
    public string Comment { get; set; }
    public Nullable<int> ObjectStateID { get; set; }
    public string StartDate { get; set; }
    public string StartTime { get; set; }
    public string Duration { get; set; }

    public virtual ReservationVersion ReservationVersion { get; set; }
}

public abstract partial class Appointment : ReservationItem
{
    public Appointment()
    {
    }

    public string AppointmentDescription { get; set; }
}

public partial class AppointedActivity : Appointment
{
    public Nullable<int> AppointedActivityID { get; set; }

    public virtual Activity Activity { get; set; }
}

public partial class AppointedDevice : Appointment
{
    public Nullable<int> AppointedDeviceID { get; set; }

    public virtual Device Device { get; set; }
}
我可以正确读取
ReservationVersion
,但是
ReservationItems
的集合每次都是空的

我的猜测是,
ReservationItems
作为一个抽象类存在问题,我发送的项目应该对应于
AppointedSomething
,它继承自
Appointment
,它继承自
ReservationItems


如果您的最后一段内容正确,我们将不胜感激。您使用的是什么序列化程序?也许您可以向序列化程序添加“提示”,在哪些派生类型上用于ReservationItem。我该怎么做?问题是我实际上需要不同类型的
reservationItem
。如果您控制json数据并使用json.Net库,那么在同一个EnumerableWell中可能存在
AppointedDevice
AppointedActivity
,看起来它可能提供了一个解决方案。如果没有,很抱歉让你偏离了真正的目标!序列化发生在控制器上:
public ReservationVersion Post([FromBody]ReservationVersion项)
我可以控制它吗?看起来像这样-请参阅。顺便问一下,这是一个WebAPI项目吗?如果是这样的话,也许值得在你的问题上加上这个标签。你在最后一段是正确的。您使用的是什么序列化程序?也许您可以向序列化程序添加“提示”,在哪些派生类型上用于ReservationItem。我该怎么做?问题是我实际上需要不同类型的
reservationItem
。如果您控制json数据并使用json.Net库,那么在同一个EnumerableWell中可能存在
AppointedDevice
AppointedActivity
,看起来它可能提供了一个解决方案。如果没有,很抱歉让你偏离了真正的目标!序列化发生在控制器上:
public ReservationVersion Post([FromBody]ReservationVersion项)
我可以控制它吗?看起来像这样-请参阅。顺便问一下,这是一个WebAPI项目吗?如果是这样的话,也许值得在你的问题上加上这个标签。
{
    "ObjectStateID": 1,
    "StartDate": "2017-10-31",
    //Other props
    "ReservationItems": [
      {
        "ReservationSummary": "Act",
        "AppointedActivityID": 95874,
        "AppointmentDescription": "ACT"
      },
      {
        "ReservationSummary": "Device",
        "AppointedDeviceID": 99040,
        "AppointmentDescription": "DEV"
      }
    ]
}