2017-11-11 120 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; 
     } 

,如果有人知道有关禁用循环请写出更好的解决方案!