2017-11-11 122 views
0

我試圖將模型發送到wcf服務。 首先我有一個序列化的問題,但我解決它通過設置實體框架4.0模型不能獲取所有數據

ContextOptions.ProxyCreationEnabled = false; 

引薦DataContractSerializer Error using Entity Framework 4.0 with WCF 4.0 但現在的模型屬性稅和產品都爲空

public ClientWindowViewModel() 
    { 
     Ip = ServerWindowViewModel.LocalIP; 
     db = new STOREDBEntities(); 
     db.Configuration.ProxyCreationEnabled = false; 
     products = db.Products;//.Where(p => p.IsSynced == false) 
    } 

產品型號

public partial class Product 
    { 
     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] 
     public Product() 
     { 
      this.Categories = new HashSet<Category>(); 
     } 

     public int Id { get; set; } 
     public string ProductName { get; set; } 
     public byte[] Image { get; set; } 
     public bool IsDeleted { get; set; } 
     public bool IsSynced { get; set; } 

     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] 
     public virtual ICollection<Category> Categories { get; set; } 
     public virtual Tax Tax { get; set; } 
    } 

發送到WCF服務器

channel.Update(checkedProducts); 

回答

0

糾正我,如果我錯了,你想通過WCF合同發送產品類,發送它後,你只能得到空值和默認值?

如果沒有在Product類中設置正確的註釋,如[DataContract],[DataMember],則無法通過WCF發送對象。

在類的上方設置[DataContract],在每個屬性的上方設置[DataMember]。沒有這個消息將不會正確序列化。

+0

是的。我找到答案見上面:) –

0

I基金,這個問題是不是在WCF.The問題是,當我設置

ContextOptions.ProxyCreationEnabled = false; 

它沒有得到產品和稅務 我必須寫 .Include("PropertyName")爲包括它

Products = dbGet.Products.Include("Categories").Include("Tax").ToList(); 

但是當我添加.Include("PropertyName")方法,當我試圖發送列表到服務器我得到了序列化中的異常(About Cycling.The產品的實例產品的產品有類別的實例......) 我解決它設置循環instacnes爲空

foreach (var item in products) 
     { 
      foreach (var ct in item.Model.Categories) 
      { 
       ct.Products = null; 
      } 
      item.Model.Tax.Product = null; 
     } 

,如果有人知道有關禁用循環請寫出更好的解決方案!