Do you have an idea how to map properties of a class to rows in a table? Let's have a table [ID, KEY, VALUE].
I would like to have for predefined key values (on attributes for example) mapped properties in a class, with values taken from value column.
EXAMPLE:
Table
 ---------------------------------
| ID  | Key        | Value        |
 ---------------------------------
| 1   | Name       | Jon          |
 ---------------------------------
| 2   | Surname    | Doe          |
 ---------------------------------
Class
public class Bar
{
    public string Name { get; set; }
    public string Suname { get; set; }
}
Is there anything in EF to achieve this or do I have to write my own custom code?
BR
You can't map values from column to properties. Thus you have key-value table, then I suggest you to read table content into dictionary.
First, you should have entity with properties which correspond to table column names:
public class Foo
{
   public string Key { get; set; }
   public string Value { get; set; }
}
After you map this entity to your table, you can read all table content into dictionary:
var values = context.Foos.ToDictionary(f => f.Key, f => f.Value);
Now you can easily get values by keys:
var name = values["Name"];
You can even use this dictionary as backing store for your properties:
public void Bar
{
    private Dictionary<string, string> values = 
         context.Foos.ToDictionary(f => f.Key, f => f.Value);
    public string Name { get { return values["Name"]; } }
    public string Surname { get { return values["Surname"]; } }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With