Example: Let's say I have these three classes. Foo
is a proper Entity Framework entity with a DbSet whereas I want my EF DbContext to be unaware of Bar
and Baz
because I have flagged Foo's Bar property with my made up SerializedColumn
attribute. By applying that attribute, I want EF to serialize the instance of Bar with its Bazes into a single string field, and transparently deserialize Bar to a Bar object when a Foo is materialized by EF.
public class Foo
{
public Guid Id { get; set; }
[SerializedColumn]
public Bar Bar { get; set; }
// ..
}
public class Bar
{
public string Name { get; set; }
public Baz[] Baz { get; set; }
// ..
}
public class Baz
{
public string Name { get; set; }
// ..
}
So Foo's table columns would look like:
[Id] [uniqueidentifier] NOT NULL
[Bar] [nvarchar](max) NULL
And when I query a Foo
I get back one with the Bar
property already deserialized. When I insert or update a Foo
the Bar
property gets serialized by EF without me having to think about it. The only thing I have to do is add the [SerializeColumn]
attribute to properties.
Goals:
Bar
property to turn into a "Bar_Id" field that points to a "Bar" table. Instead I want a nvarchar(max)
"Bar" field that will contain the serialized version of a Bar
object. If this simply isn't possible, please say so in your answer.Notes:
ComplexType
does not serve my needs. I need deep serialization and do not need to be able to filter or sort on any properties of what has been serialized.The only solution is this,
public class Foo
{
public Guid Id { get; set; }
// Not Mapped attribute will make EF
// ignore this property completely
[NotMapped]
public Bar BarObject {
get;
set;
}
public string Bar{
get{
return JsonConvert.Serialize(BarObject);
}
set{
BarObject = JsonConvert.Deserialize<BarObject>(value);
}
}
}
Updated as per suggestion by @zds
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