Map Dictionary or custom type as a nvarchar in EF Code First

ef-code-first entity-framework

Question

For a database that I now access using plain old ADO.NET with stored procedures, I want to try EF Code first.

My database contains somenvarchar(MAX) columns to and from which should be mappedDictionary<string, string> .

It is an XML-formatted string when it is stored to the database. I use this method to enable internationalization of things like a product's name in an online shop. I can't have a limit since I don't know how many languages any particular user would wish to translate to.Name for each language's column.

I chose the Dictionary - XML technique because I also wanted to avoid keeping the values in a separate database.

Currently, anytime I work with the database, I just consider any of these columns as strings. The XML may be converted into a Dictionary and returned to XML using a custom mapper function I have.

But why can't I figure out how to use EF Code first? Any thoughts?

1
7
1/23/2012 2:17:05 PM

Popular Answer

You may include a property that returns yourDictionary<,> XML string, after which you should delete the mapping for yourDictionary<,> property.

    [NotMapped]
    public Dictionary<string,string> MyDictionary
    {
     get; set;
    }

    public string DictionaryAsXml
    {
        get
        {
             return ToXml(MyDictionary);
        }
        set
        {
           MyDictionary = FromXml(value);
        }
    }

If you wish to avoid disclosing yourDictionaryAsXml View the property at this article. It demonstrates how protected and private property may be preserved.

14
1/23/2012 2:23:13 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow