【问题标题】:Serializing entity to XML - add method将实体序列化为 XML - add 方法
【发布时间】:2013-10-14 16:08:06
【问题描述】:

有人可以帮我解决以下问题吗?

模型

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.

【问题讨论】:

    标签: asp.net-mvc entity-framework datacontractserializer


    【解决方案1】:

    尝试更改此行:

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

    到这里:

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

    这将导致数据库查询运行并为您提供List&lt;IntegerList&gt; 而不是DbQuery&lt;IntegerList&gt;。这应该会提供序列化程序想要的内容(因为它有一个可用的 Add(IntegerList) 方法)。

    【讨论】:

      【解决方案2】:

      我已接受 Greg 的回答,但我觉得我应该详细说明我必须解决的后续问题:

      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.
      

      已解决:

      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.
      

      通过装饰 Integer 类的导航属性解决了如下问题:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-11
        • 1970-01-01
        • 1970-01-01
        • 2015-01-21
        • 2021-09-27
        • 2012-08-20
        相关资源
        最近更新 更多