Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF序列化:ICollection_C#_Entity Framework_Xml Serialization_Icollection - Fatal编程技术网

C# EF序列化:ICollection

C# EF序列化:ICollection,c#,entity-framework,xml-serialization,icollection,C#,Entity Framework,Xml Serialization,Icollection,我正在处理EF,我想序列化数据库中的记录并反序列化回控制台应用程序中显示它 下面是应用程序的两个类: [Serializable()] public class Product : ISerializable { public int ProductID { get; set; } public string Productname { get; set; } public Decimal? UnitPrice { get; set; } public bool

我正在处理EF,我想序列化数据库中的记录并反序列化回控制台应用程序中显示它

下面是应用程序的两个类:

[Serializable()]
public class Product : ISerializable
{
    public int ProductID { get; set; }
    public string Productname { get; set; }
    public Decimal? UnitPrice { get; set; }
    public bool Discontinued { get; set; }
    public virtual Category Category { get; set; }

    public Product() { }

    void ISerializable.GetObjectData(
        SerializationInfo info, StreamingContext context)
    {
    }
}

[Serializable()]
public class Category : ISerializable
{
    public int CategoryID { get; set; }
    public string CategoryName { get; set; }
    public string Description { get; set; }
    public byte[] Picture { get; set; }
    public virtual ICollection<Product> Products { get; set; }



    public Category() { }

    void ISerializable.GetObjectData(
        SerializationInfo info, StreamingContext context)
    {
    }
}
[Serializable()]
公共类产品:ISerializable
{
public int ProductID{get;set;}
公共字符串Productname{get;set;}
公共十进制?单价{get;set;}
公共布尔已中断{get;set;}
公共虚拟类别{get;set;}
公共产品(){}
void ISerializable.GetObjectData(
SerializationInfo,StreamingContext(上下文)
{
}
}
[可序列化()]
公共类类别:ISerializable
{
public int CategoryID{get;set;}
公共字符串CategoryName{get;set;}
公共字符串说明{get;set;}
公共字节[]图片{get;set;}
公共虚拟ICollection产品{get;set;}
公共类别(){}
void ISerializable.GetObjectData(
SerializationInfo,StreamingContext(上下文)
{
}
}
我发现,这将导致错误,因为接口无法序列化,即Category类中的产品

因此,如果我删除下面导致错误的行:

public virtual ICollection<Product> Products { get; set; }
公共虚拟ICollection产品{get;set;}
程序将成功运行。(成功序列化和反序列化)

但是,如果我删除这一行,lazyloading属性将丢失


我真的很想保留lazyloading属性,有人能给我建议一个解决方案吗。谢谢

我只想说清楚,因为这里有一些令人困惑的事情;EF通常用于持久化,但它不使用序列化;你是说这里的序列化吗?i、 e.您是否将其作为BLOB/CLOB存储在单个列中?还是将其存储在每个成员都有列的表中?第二:如果您使用的是序列化,它是哪一种?您已经标记了
xml序列化
,但是
ISerializable
/
[Serializable]
二进制格式化程序
,而不是xml;同样,您的自定义序列化实现是空的。。。所以:请澄清到底发生了什么?(我还应该注意到,您没有添加自定义反序列化构造函数,这是实现
ISerializable
时所必需的;总之,这让我强烈怀疑您混淆了序列化和持久性,并且当前所有的
[Serializable]
/
ISerializable
东西是完全冗余和未使用的),而且您也没有提到您有什么错误received@LadislavMrnka错误是“无法序列化成员类别。System.Collections.Generic.ICollection类型的产品,因为它是一个接口。因此,如果我删除我编码的行,我的程序就会成功运行。但是,如果我在网上搜索得到了正确的想法,lazyloading属性将丢失。因此,我想知道是否有任何方法可以保留lazyloading属性。@user148439请澄清:您使用的是什么序列化程序?听起来您使用的是
XmlSerializer
,但是:它没有使用
iseralizable
[Serializable]
,与EF没有任何关系。