2013-10-14 19 views
2

任何人都可以幫助我解決以下問題嗎?將實體序列化爲XML - 添加方法

模式

public class Integer 
{ 
    public int IntegerID { get; set; } 

    [Required(ErrorMessage = "Enter an integer")] 
    [Integer(ErrorMessage = "Enter an integer")] 
    public int IntegerValue { get; set; } 
    public int IntegerListID { get; set; } 

    public virtual IntegerList IntegerList { get; set; } 
} 

public class IntegerList 
{ 
    public int IntegerListID { get; set; } 
    public string Direction { get; set; } 
    public long Performance { get; set; } 
    public virtual ICollection<Integer> Integers { get; set; } 

    public IntegerList() 
    { 
     Integers = new List<Integer>(); 
    } 
} 

控制器動作

public ActionResult XMLexport() { 
     Object obj = db.IntegerLists.Include("Integers"); 
     Serialize(obj); 
     return View(); 
    } 
    public static string Serialize(Object obj) 
    { 
     DataContractSerializer serializer = new DataContractSerializer(obj.GetType()); 
     MemoryStream memoryStream = new MemoryStream(); 
     serializer.WriteObject(memoryStream, obj); 
     return Encoding.UTF8.GetString(memoryStream.GetBuffer()); 
    } 

serializer.WriteObject(memoryStream, obj); 

我得到了錯誤的行:

Type 'System.Data.Entity.Infrastructure.DbQuery`1[[IntegerSorter.Models.IntegerList,  IntegerSorter, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' is an invalid collection type since it does not have a valid Add method with parameter of type 'IntegerSorter.Models.IntegerList'. 

有人可以告訴我在哪裏以及如何實現Add方法嗎?

更新:

發生變化:

Object obj = db.IntegerLists.Include("Integers"); 

Object obj = db.IntegerLists.Include("Integers").ToList(); 

結果:

Type 'System.Data.Entity.DynamicProxies.IntegerList_62F0932A6DFC38A25629DF18911498D42B3785A93BCE8B8D2F77C3363B3F4200' with data contract name 'IntegerList_62F0932A6DFC38A25629DF18911498D42B3785A93BCE8B8D2F77C3363B3F4200:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer. 

回答

2

嘗試改變這一行:

Object obj = db.IntegerLists.Include("Integers");

要這樣:

Object obj = db.IntegerLists.Include("Integers").ToList();

這將導致數據庫查詢運行,給你一個List<IntegerList>而不是DbQuery<IntegerList>。這應該提供序列化器所需的東西(因爲它有一個Add(IntegerList)方法可以根據錯誤的要求)。

+0

根據此答案更新的問題 – user1405195

0

我已經接受Greg的答案,但覺得我應該詳細的後續問題,我不得不努力通過:

Context.Configuration.ProxyCreationEnabled = false; 

然後:

Object graph for type 'System.Collections.Generic.List`1[[IntegerSorter.Models.Integer, IntegerSorter, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' contains cycles and cannot be serialized if reference tracking is disabled. 

Type 'System.Data.Entity.DynamicProxies.IntegerList_62F0932A6DFC38A25629DF18911498D42B3785A93BCE8B8D2F77C3363B3F4200' with data contract name 'IntegerList_62F0932A6DFC38A25629DF18911498D42B3785A93BCE8B8D2F77C3363B3F4200:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer. 

與解決

通過裝飾整數類的導航屬性解決如下:

[IgnoreDataMember] 
public virtual IntegerList IntegerList { get; set; }