Say I have the following class:
struct Vector
{
public float X { get; set; }
public float Y { get; set; }
public float Z { get; set; }
}
class Player
{
public string Name { get; set; }
public Vector Position { get; set; }
}
How do I configure that in Entity Framework (core) such that it maps to Name, PositionX, PositionY, PositionZ?
This is for code generation purposes, so I don't want the user having to create their POCO's with EF in mind (it emits to a whole lot of other languages too!)
Currently (EF Core 3) it is not supported. However, there is a GitHub issue about it and looks like structs-as-owned-types has been accepted for future versions:
https://github.com/dotnet/efcore/issues/9906
Was looking for the same thing, and came across this question. Thought I'd post what I found: The EF Core team suggest to store it as JSON in your database and use a custom value convertor:
modelBuilder.Entity<Order>()
.Property(e => e.Vector)
.HasConversion(
v => JsonSerializer.Serialize(v, null),
v => JsonSerializer.Deserialize<Vector>(v, null));
Not ideal though.
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