问题描述
我有一个类,我想保留元数据 - 有一些交互方案,所以meta允许我为不同的交互类型保留不同的元。 class Feed()
{
Guid FeedId {get;组; }
ObjectMetaDictionary Meta {get;组;我想要EF来序列化这个ObjectMetaDictionary,并将其存储为一个字符串/数据库中的VarChar。当我检索一个记录时,我希望它被反序列化为一个ObjectMetaDictionary。
EF是否支持这个?我如何做?
我正在使用实体框架代码。
解决方案:我在下面提供了一个解决我的问题的答案。
解决方案显然这实际上很简单,我会接受这个答案。我能够得到它的工作,由于以前的一些帮助。
中的流畅配置OnModelCreating
允许我们告诉EF什么用作序列化到数据库的值属性,并再次退出。
这是我的解决方案:
public class Feed
{
public virtual Guid FeedId {get;组; }
public virtual FeedMetaData Meta {get;组; }
public virtual string标题{get;组; }
public virtual string描述{get;组; }
}
public class FeedMetaData
{
public Dictionary< string,string>数据{get;组;
public string Serialized
{
get {return JsonConvert.SerializeObject(Data); }
set
{
if(string.IsNullOrEmpty(value))return;
var metaData = JsonConvert.DeserializeObject< Dictionary< string,string>>(value);
Data = metaData?新字典< string,string>();
}
}
// addl code removed ...
}
public class FeedsDbContext:DbContext
{
public DbSet< Feed>饲料{get;组;
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.ComplexType< FeedMetaData>()
.Property(p => p.Serialized)
.HasColumnName(Meta);
modelBuilder.ComplexType< FeedMetaData>()。Ignore(p => p.Data);
base.OnModelCreating(modelBuilder);
}
}
I have a class that I want to keep meta data for -- there a several interaction scenarios so meta allows me to keep different meta for different interaction types.
class Feed()
{
Guid FeedId { get; set; }
ObjectMetaDictionary Meta { get; set; }
}
I would like EF to serialize this ObjectMetaDictionary and store it as a string/VarChar in the database. When I retrieve a record I want it to be deserialized as an ObjectMetaDictionary.
Does EF support this? How can I do it?
I am using Entity Framework Code First.
SOLVED: I provided an answer below that solved my problem. I will accept this answer as soon as SO allows me to.
解决方案 Apparently this is actually quite easy. I was able to get it working thanks to some help from this previous SO answer.
Fluent configuration in OnModelCreating
allows us to tell EF what to use as the value property for serializing to the DB and back out again.
Here's my solution:
public class Feed
{
public virtual Guid FeedId { get; set; }
public virtual FeedMetaData Meta { get; set; }
public virtual string Title { get; set; }
public virtual string Description { get; set; }
}
public class FeedMetaData
{
public Dictionary<string, string> Data { get; set; }
public string Serialized
{
get { return JsonConvert.SerializeObject(Data); }
set
{
if(string.IsNullOrEmpty(value)) return;
var metaData = JsonConvert.DeserializeObject<Dictionary<string, string>>(value);
Data = metaData ?? new Dictionary<string, string>();
}
}
// addl code removed...
}
public class FeedsDbContext : DbContext
{
public DbSet<Feed> Feeds { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.ComplexType<FeedMetaData>()
.Property(p => p.Serialized)
.HasColumnName("Meta");
modelBuilder.ComplexType<FeedMetaData>().Ignore(p => p.Data);
base.OnModelCreating(modelBuilder);
}
}
这篇关于可以将对象嵌入到EF实体中(序列化保存,反序列化访问)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!