Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

ef code first - Can I Embed an object in an EF entity (serialize on save, deserialize on access)?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...