【发布时间】:2018-04-18 11:54:00
【问题描述】:
我有这个实体类:
public class Foo{
[Key]
[Column("ENT_ID")]
public int Id { set; get; }
.....
[NotMapped]
public virtual Employee Employee{ set; get; }
[NotMapped]
public virtual List<Car> Cars{ set; get; }
[NotMapped]
public virtual List<Book> Books{ set; get; }
public Foo()
{
Books= new List<Book>();
Cars= new List<Car>();
}
}
我想从序列化中排除虚拟属性,使用 [NotMapped] 有效,但我无法导航到此属性! 我尝试使用 [XMLIgnore] 但没有帮助。
我使用这个序列化类:
public class XmlSerializer<T> where T : class
{
public static string Serialize(T data)
{
var sw = new StringWriter();
var xmlSerializer = new XmlSerializer(typeof(T));
xmlSerializer.Serialize(sw, data);
return sw.ToString();
}
public static T Deserialize(string data)
{
var ser = new XmlSerializer(typeof(T));
var sr = new StringReader(data);
return (T)ser.Deserialize(sr);
}
}
如何从序列化中排除虚拟属性并仍然将它们用作导航属性?
【问题讨论】:
标签: entity-framework serialization