我正在尝试序列化数据库模型。但是有一个错误。
这是我的模特:
public class Equipment
{
public int Id { get; set; }
//Двигатель
[Required]
[Display(Name = "Двигатель")]
public int Engine { get; set; }
//Количество лошадиных сил
[Required]
[Display(Name = "Мощность")]
public int Power { get; set; }
//Год выпуска
[Required]
[Display(Name = "Год выпуска")]
public int ReleaseYear { get; set; }
//Тип привода
[Required]
[Display(Name = "Тип привода")]
public string DriveType { get; set; }
//КПП
[Required]
[Display(Name = "КПП")]
public string Transmission { get; set; }
//Кузов
[Required]
[Display(Name = "Кузов")]
public string Body { get; set; }
//Максимальная скорость
[Required]
[Display(Name = "Максимальная скорость")]
public int MaxSpeed { get; set; }
//Вес
[Required]
[Display(Name = "Вес")]
public int Weight { get; set; }
//Бак
[Required]
[Display(Name = "Бак")]
public int MaxFuelVolume { get; set; }
//Цвет автомобиля
[Required]
[Display(Name = "Цвет")]
public string Color { get; set; }
//Изорбражение автомобиля
[Required]
[Display(Name = "Изображение автомобиля")]
public string Picture { get; set; }
public int CarModelId { get; set; }
public virtual CarModel CarModel { get; set; }
public virtual ICollection<Car> Cars { get; set; }
public Equipment()
{
Cars = new List<Car>();
}
}
这是我在控制器中用于序列化的代码:
return Json(JsonConvert.SerializeObject(car.Equipment));
这是错误产生的原因:
检测到类型为'System.Data.Entity.DynamicProxies.Equipment_4B2DAA6CED521984C9D15F5FEA2BB989D5DB7F80683721A0FC847726CB1C9ACB'的自引用循环。路径“ CarModel.Equipments”。
这是上课的人:
public class CarModel
{
public int Id { get; set; }
[Display(Name = "Модель")]
public string ModelName { get; set; }
public int MarkId { get; set; }
public virtual Mark Mark { get; set; }
public virtual ICollection<Equipment> Equipments { get; set; }
public CarModel()
{
Equipments = new List<Equipment>();
}
}
我尝试连接System.web.script.serialization
并将脚本忽略属性设置为
public virtual CarModel CarModel { get; set; }
public virtual ICollection<Car> Cars { get; set; }
在装备课上及以后
public virtual ICollection<Equipment> Equipments { get; set; }
在CarModel类中,它仍然是相同的错误。
请告诉我如何避免此错误?以及如何序列化具有循环依赖性的模型?
ajax请求:
$('#autoList').on('change', function () {
var ddlAuto = $('#autoList').val();
if (!ddlAuto || ddlAuto === '')
return false;
$.ajax({
type: "POST",
url: "/Administration/GetCarInfo",
data: {
"carId": ddlAuto
},
success: function (data) {
alert('ОК');
}
});
});
但是在一种情况下,可以序列化,尽管它不是序列化为对象而是字符串。
序列化的结果,如果在控制器中写
return Json(JsonConvert.SerializeObject(car.Equipment, Formatting.Indented, new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.Objects
}));
"{
"$id": "1",
"CarModel": {
"$id": "2",
"Equipments": [
{
"$ref": "1"
}
],
"Mark": {
"$id": "3",
"CarModels": [
{
"$ref": "2"
},
{
"$id": "4",
"Equipments": [
{
"$id": "5",
"CarModel": {
"$ref": "4"
},
"Cars": [
{
"$id": "6",
"Equipment": {
"$ref": "5"
},
"Orders": [
{
"$id": "7",
"Cars": {
"$ref": "6"
},
"Clients": {
"$id": "8",
"Orders": [
{
"$ref": "7"
},
{
"$id": "9",
"Cars": {
"$id": "10",
"Equipment": {
"$id": "11",
"CarModel": {
"$ref": "4"
},
"Cars": [
{
"$ref": "10"
}
],
"Id": 30,
"Engine": 2500,
"Power": 170,
"ReleaseYear": 2011,
"DriveType": "Передний",
"Transmission": "Автомат",
"Body": "Седан",
"MaxSpeed": 220,
"Weight": 1800,
"MaxFuelVolume": 80,
"Color": "Чёрный"
我试图做其他事情
return Json(JsonConvert.SerializeObject(car.Equipment, Formatting.Indented, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
}));
然后发生错误:
“ System.OutOfMemoryException”
请告诉我我做错了什么?如何将模型序列化为对象以继续使用它?