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

时间:2017-10-31 21:14:49

标签: 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 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; }
}

我正在使用Entity Framework,我无法理解为什么我无法从JSON帖子中正确解析复杂对象。

这是发送的JSON(有效ReservationVersion):

{
    "ObjectStateID": 1,
    "StartDate": "2017-10-31",
    //Other props
    "ReservationItems": [
      {
        "ReservationSummary": "Act",
        "AppointedActivityID": 95874,
        "AppointmentDescription": "ACT"
      },
      {
        "ReservationSummary": "Device",
        "AppointedDeviceID": 99040,
        "AppointmentDescription": "DEV"
      }
    ]
}

我可以正确阅读ReservationVersion,但ReservationItems的集合每次都是空的。

我的猜测是ReservationItems是一个抽象类的问题,我发送的项应该对应AppointedSomething,它继承自Appointment,继承自{{ 1}}

非常感谢任何帮助

0 个答案:

没有答案