質問

Can anybody help me with the below?

Models

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>();
    }
}

Controller Action

    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());
    }

At the line

serializer.WriteObject(memoryStream, obj);

I'm getting the error:

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'.

Can somebody advise me where and how to implement the Add method?

Update:

Changing:

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

to

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

results in:

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.
役に立ちましたか?

解決

Try changing this line:

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

To this:

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

This will cause the database query to run and give you a List<IntegerList> instead of a DbQuery<IntegerList>. This should then provide what the serializer wants (because it has a Add(IntegerList) method available as requested by the error).

他のヒント

I've accepted Greg's answer but feel I should detail the subsequent issues I had to work through:

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.

was solved with:

Context.Configuration.ProxyCreationEnabled = false;

and then:

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.

was solved by decorating the navigation property on the Integer class as below:

[IgnoreDataMember]
public virtual IntegerList IntegerList { get; set; }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top