I'm building an Entity Framework data context for a pre-existing application. As this application has a nice clean set of POCOs that define the domain model, I opted to use EF's Code First to generate the database from these classes so that we don't have to maintain two separate models; in theory the database will be migrated to match whatever we do to the POCOs, with just the occasional bit of mapping required in the OnModelCreating()
override.
My problem is that one of the domain classes has a property of type Dictionary<string, object>
that's being used to store state data (which is being read from a key-value store elsewhere, so we're stuck with that format). My first thought was that we could just apply [NotMapped]
to this property, and then create a string
property in which the getter and setter just serialised/deserialised the Dictionary
to XML, JSON or whatever.
The issue is that we're not changing the domain classes with database-specific stuff, so I have to do all that from the data access assembly. I know how to apply the NotMapped
attribute in the Fluent Configuration API, but I can't work out how to set up a column for the serialised parameters. On the model, it would look like this:
[NotMapped]
public IDictionary<string, object> Parameters { get; set; }
public string JSONParameters
{
get
{
return JsonConvert.SerializeObject(Parameters);
}
set
{
Parameters = JsonConvert.DeserializeObject<Dictionary<string, object>>(value);
}
}
How would I write the equivalent code in the Fluent Configuration API? Is it even possible?