2017-06-14 50 views
1

我有一個類workHour,我試圖以serialize但它只返回ID (即{ID:1})C#序列化對象只返回ID

public class WorkHour : DomainEntity<int> 
{ 
    public virtual Day day { get; set; } 

    [Key] 
    public int id; 

    [Required] 
    public int dayId { get; set; } 
    public TimeSpan startTime { get; set; } 
    public TimeSpan endTime { get; set; } 
    public DateTime created { get; set; } 
    public bool deleted { get; set; } 
} 

對象之前結果序列

|created |{6/14/2017 4:23:50 AM}| 
|day  |null|  
|dayId |1| 
|deleted |false| 
|endTime |{04:23:50.5419087}| 
|id  |0| 
|startTime|{04:23:50.5419087}| 

的返回JSON(結果)其結果是:

{"Id": 18} 
+7

顯示的序列化代碼 –

回答

1

此代碼的工作很好,但我沒有一個父類:

public class WorkHour //: DomainEntity<int> 
    { 
     //public virtual Day day { get; set; } 

     [Key] 
     public int id; 

     [Required] 
     public int dayId { get; set; } 
     public TimeSpan startTime { get; set; } 
     public TimeSpan endTime { get; set; } 
     public DateTime created { get; set; } 
     public bool deleted { get; set; } 
    } 


    public class Test 
    { 
     public void Main() 
     { 
      var o = new WorkHour() 
      { 
       created = DateTime.Now, 
       dayId = 0, 
       deleted = false, 
       endTime = new TimeSpan(0, 0, 2), 
       startTime = new TimeSpan(0, 0, 2), 
       id = 0, 
      }; 
      Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(o)); 

     } 

    } 

newtonsoft.json

+0

對於任何人誰不熟悉'Newtonsoft.Json',也不會明確至於'JsonConvert'是什麼。 – ColinM