简体   繁体   English

EntityFramework 6序列化

[英]EntityFramework 6 Serialize

I have searched in google for 3 days without any success. 我已经在Google搜索了3天,但没有成功。

I am using the Database First approach then generated the classes. 我正在使用数据库优先方法,然后生成了类。 What I need to do now is to Serialize my entity into Json and then either save to a file or send to another party using Web API (certainly they will be deserialized and consumed later) 我现在需要做的是将我的实体序列化为Json,然后保存到文件或使用Web API发送给另一方(肯定会反序列化并在以后使用)

The problem here is that EF6 trying to includes all the Navigation Properties, and makes the serialization / deserialization extremely difficults. 这里的问题是EF6试图包含所有导航属性,并使序列化/反序列化变得极为困难。 Setting the properties below doesn't work at all. 在下面设置属性根本不起作用。

Configuration.LazyLoadingEnabled = false;
Configuration.ProxyCreationEnabled = false;

Is there an easy way telling EF6 to just ignore those Navigation Properties all together when serializing? 有没有一种简单的方法告诉EF6在序列化时完全忽略那些导航属性? Since I am using the DB first approaches, I have access to all related tables with their foreign keys. 由于我使用的是数据库优先方法,因此我可以使用其外键访问所有相关表。

Thanks in Advance. 提前致谢。

Try using Newtonsoft.Json; 尝试using Newtonsoft.Json; nuget for the serialization. nuget进行序列化。 To serialize: string json = JsonConvert.SerializeObject(MyObjects, Formatting.Indented); 序列化: string json = JsonConvert.SerializeObject(MyObjects, Formatting.Indented); Inside your class MyObject , you just add a [JsonIgnore] annotation above properties you want to skip (Navigation props among others) class MyObject ,您只需在要跳过的属性上方添加[JsonIgnore]批注(导航道具等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM