2

我对 protobuf-net 和泛型的使用有疑问。

鉴于:

    [DataContract]
public class CacheData
{
    [DataMember(Order = 1)]
    public List<CacheLoadItem<int>> Foo;

    [DataMember(Order = 2)]
    public List<CacheLoadItem<int>> Bar;

    [DataMember(Order = 3)]
    public List<CacheLoadItem<int>> XXX;

    [DataMember(Order = 4)]
    public List<CacheLoadItem<string>> YYY;

    [DataMember(Order = 5)]
    public List<CacheLoadItem<int>> Other;

    [DataMember(Order = 6)]
    public List<CacheLoadItem<int>> Other2;

    [DataMember(Order = 7)]
    public List<CacheLoadItem<int>> Other3;

    [DataMember(Order = 8)]
    public List<CacheLoadItem<string>> EvenMore;

    [DataMember(Order = 9)]
    public List<CacheLoadItem<string>> AlmostThere;
}

[DataContract]
public class CacheLoadItem<V>
{
    [DataMember(Order = 1)]
    public int ID;

    [DataMember(Order = 2)]
    public string Name;

    [DataMember(Order = 3)]
    public V Value;
}

具有通用 int 列表的 CacheLoadItem 可以很好地序列化,但具有通用字符串列表的 CacheLoadItem 不会。

我认为这与通用列表首先被序列化有关。

具有通用字符串列表的 CacheLoadItem 包含正确数量的项目,但具有默认/空值。

有谁知道为什么会这样?

4

1 回答 1

0

好的,这绝对是实体框架相关的。

CacheData 是用 LINQtoEF 提取的。

喜欢:

                        using (var myDatabase = new MyDatabase(entityBuilder.ToString()))
        {
            result.A = (from a in myDatabase.ATable select new CacheLoadItem<int> { ID = a.ID, Name = a.Name, Value = a.Number }).ToList();
            result.B = (from b in myDatabase.BTable select new CacheLoadItem<string> { ID = b.ID, Name = b.Name, Value = b.Code }).ToList();
            result.C = (from c in myDatabase.CTable select new CacheLoadItem<int> { ID = c.ID, Name = c.Name, Value = c.ID }).ToList();
            result.D = (from d in myDatabase.DTable select new CacheLoadItem<int> { ID = d.ID, Name = d.Name, Value = d.Number }).ToList();
            result.E = (from e in myDatabaseETable select new CacheLoadItem<int> { ID = e.ID, Name = e.Name, Value = e.Number }).ToList();
            result.F = (from f in myDatabase.FTable select new CacheLoadItem<string> { ID = f.ID, Name = f.Name, Value = f.Number }).ToList();
            result.G = (from g in myDatabaseGTable select new CacheLoadItem<string> { ID = g.ID, Name = g.Name, Value = g.Code }).ToList();
            result.H = (from h in myDatabaseHTable select new CacheLoadItem<int> { ID = h.ID, Name = h.Name, Value = h.Number }).ToList();
            result.I = (from i in myDatabaseITable select new CacheLoadItem<int> { ID = i.ID, Name = i.Name, Value = i.Number }).ToList();
        }

不起作用,但以下工作。

            using (var myDatabase = new MyDatabase(entityBuilder.ToString()))
        {
            result.A = (from a in myDatabase.ATable select a).ToList().Select(b => new CacheLoadItem<int> { ID = a.ID, Name = a.Name, Value = a.Number }).ToList();
            result.B = (from b in myDatabase.BTable select b).ToList().Select(b=>new CacheLoadItem<string> { ID = b.ID, Name = b.Name, Value = b.Code }).ToList();
            result.C = (from c in myDatabase.CTable select c).ToList().Select(c=> new CacheLoadItem<int> { ID = c.ID, Name = c.Name, Value = c.ID }).ToList();
            result.D = (from d in myDatabase.DTable select d).ToList().Select(d=> new CacheLoadItem<int> { ID = d.ID, Name = d.Name, Value = d.Number }).ToList();
            result.E = (from e in myDatabaseETable select e).ToList().Select(e=> new CacheLoadItem<int> { ID = e.ID, Name = e.Name, Value = e.Number }).ToList();
            result.F = (from f in myDatabase.FTable select f).ToList().Select(f => new CacheLoadItem<string> { ID = f.ID, Name = f.Name, Value = f.Number }).ToList();
            result.G = (from g in myDatabaseGTable select g).ToList().Select(g=> new CacheLoadItem<string> { ID = g.ID, Name = g.Name, Value = g.Code }).ToList();
            result.H = (from h in myDatabaseHTable select h).ToList().Select(h=> new CacheLoadItem<int> { ID = h.ID, Name = h.Name, Value = h.Number }).ToList();
            result.I = (from i in myDatabaseITable select i).ToList().Select(i=> new CacheLoadItem<int> { ID = i.ID, Name = i.Name, Value = i.Number }).ToList();
        }

不同之处在于我不在 LinqToEF 中进行转换,而是在 LINQtoObjects 中进行。

所以我猜EF坏了?

于 2010-06-21T12:51:55.587 回答