首页 新闻 搜索 专区 学院

EntityFramework查询返回结果序列化出错

0
悬赏园豆:20 [已关闭问题] 关闭于 2012-07-19 09:57

我现在有两个类,主从关系,比如下面这样:

//物品类
[Serializable]
public partial class ProductInfo
{
public ProductInfo(){}

public string ID{get; set;}

public string Name{get; set;}

[Association(
"ProductFactory","ID","ProductID")]
public virtual ICollection<ProductFactoryInfo> ProductFactoriess{ get; set; }
}

//物品厂商类
[Serializable]
public partial class ProductFactoryInfo
{
public ProductFactoryInfo(){}


public int ID{get;set;}

public string ProductID{get;set;}

[Association(
"ProductInfo", "ProductID", "ID")]
public virtual ProductInfo Product { get; set; }
}

我现在通过dbcontext查询出来得到一个List<ProductInfo>,在进行序列化的时候总是出错:

static void Main(string[] args)
{
var products
= GetProducts();
//这里总是报错
var rss = JsonWithSettings(products);
Console.WriteLine(rss.ToString());
Console.ReadKey();
}

//获取物品列表
static IList<ProductInfo> GetProducts()
{
DatabaseContext db
= new DatabaseContext();
//加了这一句,不会报错,但是序列化的时候ProductFactories也变成null了
db.Configuration.ProxyCreationEnabled = false;
var products
= db.Products;
return products.ToList();
}

//使用的是Json.Net进行序列化
static string JsonWithSettings(IList<ProductInfo> products)
{
JsonSerializerSettings jsSettings
= new JsonSerializerSettings();
jsSettings.ReferenceLoopHandling
= ReferenceLoopHandling.Ignore;

var rss
= JsonConvert.SerializeObject(products, Formatting.None, jsSettings);
return rss.ToString();
}

下面是具体的错误内容:

捕捉到 System.Reflection.TargetInvocationException
Message
=调用的目标发生了异常。
Source
=mscorlib
StackTrace:
在 System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct
& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
在 System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
在 System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
在 Newtonsoft.Json.Serialization.JsonContract.InvokeOnSerializing(Object o, StreamingContext context)
在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContract collectionValueContract)
在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContract collectionValueContract)
在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.WriteMemberInfoProperty(JsonWriter writer, Object memberValue, JsonProperty property, JsonContract contract)
在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContract collectionValueContract)
在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContract collectionValueContract)
在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.WriteMemberInfoProperty(JsonWriter writer, Object memberValue, JsonProperty property, JsonContract contract)
在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContract collectionValueContract)
在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContract collectionValueContract)
在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContract collectionValueContract)
在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContract collectionValueContract)
在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value)
在 Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value)
在 Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)
在 Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Formatting formatting, JsonSerializerSettings settings)
InnerException: System.InvalidOperationException
Message
=无法将 RelationshipManager 对象序列化。当 RelationshipManager 属于不实现 IEntityWithRelationships 的实体对象时,无法将此类型的对象序列化。
Source
=System.Data.Entity
StackTrace:
在 System.Data.Objects.DataClasses.RelationshipManager.OnSerializing(StreamingContext context)
InnerException:

在线等,任何指点都非常感激,谢谢...

随处走走的主页 随处走走 | 初学一级 | 园豆:123
提问于:2011-04-21 17:19
< >
分享
所有回答(3)
0

建议用Code First, 实体类用POCO。

dudu | 园豆:36406 (高人七级) | 2011-04-21 20:07
我是用的Code First,实体类用的是POCO, 代码上应该能看出来吧...
支持(0) 反对(0) 随处走走 | 园豆:123 (初学一级) | 2011-04-21 21:09
0

可以尝试下将实体的外键线删除,在序列化的时候会序列化外键关联的实体。

LoveJenny | 园豆:532 (小虾三级) | 2011-04-21 21:57
是的, 没有外键是可以的,我现在希望能够将外键序列化进去,如果没有数据就算了...
支持(0) 反对(0) 随处走走 | 园豆:123 (初学一级) | 2011-04-21 22:40
0

我这边是这样修改的

return ResponseHelper.Serialize(

                    list,

                    responseFormat,

                    new JsonSerializerSettings()

                    {

                        //ContractResolver = new EntityObjectContractResolver();原先是这句,修改是换成了下面一句

                PreserveReferencesHandling = PreserveReferencesHandling.Objects

                    }

                );

其心塞渊 | 园豆:202 (菜鸟二级) | 2012-08-23 09:51

刚才写的是没有用POCO,添加了之后是在Model1.Context.cs中修改的,

public XXXX()
: base(ConnectionString, ContainerName)
{
this.ContextOptions.LazyLoadingEnabled = true;
this.ContextOptions.ProxyCreationEnabled = false;//加了这一句
}

支持(0) 反对(0) 其心塞渊 | 园豆:202 (菜鸟二级) | 2012-08-23 11:13
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册