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

后端 未结 3 1091
广开言路
广开言路 2020-12-08 03:08

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.



        
3条回答
  •  醉梦人生
    2020-12-08 03:38

    Have your Entity Framework object be simple and have a String property for the column in the database.

    class Feed()
    {
        Guid FeedId { get; set; }
        String Meta { get; set; }
    }
    

    Create methods that save and load the property as such: (it's been a while since I've used EF, so i'm not sure if you can create a transient property with these getter/setters or if you need something else)

    //Reading from string column Meta
    (ObjectMetaDictionary) XamlServices.Load(new StringReader(someFeed.Meta));
    
    //Saving to string column Meta
    someFeed.Meta = XamlServices.Save(value);
    

    This brings another whole issue to your project though. Changing your ObjectMetaDictionary might cause it to not deserialize from the database correctly. Your ObjectMetaDictionary becomes essentially part of your database schema and you will need to handle versioning or changes accordingly.

提交回复
热议问题