我现在有两个类,主从关系,比如下面这样:
//物品类
[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:
在线等,任何指点都非常感激,谢谢...
建议用Code First, 实体类用POCO。
可以尝试下将实体的外键线删除,在序列化的时候会序列化外键关联的实体。
我这边是这样修改的
return ResponseHelper.Serialize(
list,
responseFormat,
new JsonSerializerSettings()
{
//ContractResolver = new EntityObjectContractResolver();原先是这句,修改是换成了下面一句
PreserveReferencesHandling = PreserveReferencesHandling.Objects
}
);
刚才写的是没有用POCO,添加了之后是在Model1.Context.cs中修改的,
public XXXX()
: base(ConnectionString, ContainerName)
{
this.ContextOptions.LazyLoadingEnabled = true;
this.ContextOptions.ProxyCreationEnabled = false;//加了这一句
}