Reputation: 1754
I have a question about HasConversion in EFCore. I decided to save in DB a model like a json String then I decided to implement automatic conversion of this Object with .HasConversion method in Configure Method of EFCore.
I have use this method in with Enum-> String and all run perfect but using this approach with object the situation become more complex:
I simplify scenario:
public class ObjectA
{
public string A {get; set;}
}
public partial class EntityA
{
public ObjectA objectA {get; set;} //this is my json object in DB
}
So I have a column in DB nvchar that i want to converto to ObjectA when I extract from DB. As EF core documentation say, i implemented a conversion class
public void Configure(EntityTypeBuilder<EntityA> builder)
{
builder.Property(x => x.ObjectA)
.HasConversion(y => JsonSerializer.Serialize(
y,
new JsonSerializerOptions()
),
y => JsonSerializer.Deserialize<ItemList>(
y,
new JsonSerializerOptions()
)
);
}
and when this mapper run all seams ok. But i use it in query for redeem object and convert it i have this error.
The entity type 'ObjectA' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'.
but ObjectA is not an Entity/Table so where is the problem?
I have same error even if i try to add-Merge theoretically EF should read conversion and it shouldn't automatically see the AbjectA as an entity but simply a string.
Sorry for my english i hope is all clear.
Tanx in advice
Upvotes: 0
Views: 1006
Reputation: 1754
Sorry, after returning the project and fixing the code the error would show up again. I then understood that the error was due to the fact that I had added the same ObjectA object in a new model but I had not inserted the Mapper in the IEntityTypeConfiguration.
So my code is correct. Thank you all
Upvotes: 0