I am working with Entity Framework Code first. I have a simple model:
public class Variable
{
public string Name { get; set; }
public int Id { get;
No, EF doesn't have any type converters or custom type mappers as alternative to model binders from MVC. You must always use some hack to force persistence. Another way to do the same is mapping TextOptions
as collection of related entities. It will make your separation of concerns better but it will complicate your model and working with Variable
.
public class Variable
{
public string Name { get; set; }
public int Id { get; set; }
public IList<TextOption> TextOptions
{
get;
set;
}
}
public class TextOption
{
public int Id { get; set; }
public string Text { get; set; }
}
A 3rd option would be to do the serialization using JSON.NET.
I ran some performance tests for write scenarios using the 3 options listed on this thread (String split serialization, JSON.NET, and introducing an entity) and found that JSON.NET yields the best performance.
Preliminary write results of 200 equal entities (see source code here and run the test yourself):
Sample using JSON serializer:
public class VariableJson
{
public string Name { get; set; }
public int Id { get; set; }
public virtual IList<string> TextOptions
{
get
{
return _TextOptions;
}
set
{
_TextOptions = value;
}
}
private IList<string> _TextOptions;
public string TextOptionsSerialized
{
get
{
return JsonConvert.SerializeObject(_TextOptions);
}
set
{
_TextOptions = JsonConvert.DeserializeObject<IList<string>>(value);
}
}
}