使用 Entity Framework 将 Collection 另存为 JSON
Save Collection As JSON with Entity Framework
我正在尝试找到一种方法来获得一个 object 集合,但是当它保存到数据库时变成了一个 JSON 字符串。我如何设置 entity framework 6.1 来执行此操作?示例:
public class Company{
public Company(){
this.Times = new HashSet<DateTime>();
}
public int Id {get;set;}
public string Name {get;set;}
public List<DateTime> Times {get;set;}
}
公司是一个实体object。我希望将 Times 作为 json 次字符串存储在数据库中。我希望它在从数据库读取时序列化为日期时间列表。我希望将保存的列表转换回 json 字符串并保存。
以下应该可以工作(我使用 Json.Net,但您可以将其更改为任何其他序列化程序):
public class Company
{
public int Id {get;set;}
public string Name {get;set;}
[NotMapped]
public List<DateTime> Times {get;set;}
[Column("Times")]
public string TimesSerialized
{
get
{
return JsonConvert.SerializeObject(Times);
}
set
{
Times = string.IsNullOrEmpty(value)
? new List<DateTime>()
: JsonConvert.DeserializeObject<List<DateTime>>(value);
}
}
}
如果手动映射,也可以将 TimesSerialized 设为私有。
已接受答案的问题是,对列表内容的任何更改(添加、修改或删除条目)不会被 EF 跟踪。
这是我的解决方案,灵感来自 this excellent blog post。
这个 class 负责序列化和反序列化,以便集合可以存储在父模型的单个列中:
[ComplexType]
public class DateTimeCollection : Collection<DateTime>
{
public void AddRange(IEnumerable<DateTime> collection)
{
foreach (var item in collection)
{
Add(item);
}
}
[Column("Times")]
public string Serialized
{
get { return JsonConvert.SerializeObject(this); }
private set
{
if (string.IsNullOrEmpty(value))
{
Clear();
return;
}
var items = JsonConvert.DeserializeObject<DateTime[]>(value);
Clear();
AddRange(items);
}
}
}
就是这样!您现在可以完全按照您的预期在父 class 上使用这个新集合。将跟踪集合内容的更改。
public class Company{
// ...
public DateTimeCollection Times { get; set; }
}
我正在尝试找到一种方法来获得一个 object 集合,但是当它保存到数据库时变成了一个 JSON 字符串。我如何设置 entity framework 6.1 来执行此操作?示例:
public class Company{
public Company(){
this.Times = new HashSet<DateTime>();
}
public int Id {get;set;}
public string Name {get;set;}
public List<DateTime> Times {get;set;}
}
公司是一个实体object。我希望将 Times 作为 json 次字符串存储在数据库中。我希望它在从数据库读取时序列化为日期时间列表。我希望将保存的列表转换回 json 字符串并保存。
以下应该可以工作(我使用 Json.Net,但您可以将其更改为任何其他序列化程序):
public class Company
{
public int Id {get;set;}
public string Name {get;set;}
[NotMapped]
public List<DateTime> Times {get;set;}
[Column("Times")]
public string TimesSerialized
{
get
{
return JsonConvert.SerializeObject(Times);
}
set
{
Times = string.IsNullOrEmpty(value)
? new List<DateTime>()
: JsonConvert.DeserializeObject<List<DateTime>>(value);
}
}
}
如果手动映射,也可以将 TimesSerialized 设为私有。
已接受答案的问题是,对列表内容的任何更改(添加、修改或删除条目)不会被 EF 跟踪。
这是我的解决方案,灵感来自 this excellent blog post。
这个 class 负责序列化和反序列化,以便集合可以存储在父模型的单个列中:
[ComplexType]
public class DateTimeCollection : Collection<DateTime>
{
public void AddRange(IEnumerable<DateTime> collection)
{
foreach (var item in collection)
{
Add(item);
}
}
[Column("Times")]
public string Serialized
{
get { return JsonConvert.SerializeObject(this); }
private set
{
if (string.IsNullOrEmpty(value))
{
Clear();
return;
}
var items = JsonConvert.DeserializeObject<DateTime[]>(value);
Clear();
AddRange(items);
}
}
}
就是这样!您现在可以完全按照您的预期在父 class 上使用这个新集合。将跟踪集合内容的更改。
public class Company{
// ...
public DateTimeCollection Times { get; set; }
}