❔ Saving a JSON object to Database
HI. I want to save a Array to Database but I'm this error.
This is the body
Any help will be much appreciated
24 Replies
GitHub
ELIXIR-DEPOT/ChecklistRepository.cs at developmentenv · aldrin0408a...
A system that provides barcode technology to streamline the process and data visibility by tracking of inventories in Central Depot Plant - ELIXIR-DEPOT/ChecklistRepository.cs at developmentenv · a...
is there a reason you aren't using EF value converters?
I was about to ask the same, why the
Values
property?Just new to this and I just found this approach to the internet
Value Conversions - EF Core
Configuring value converters in an Entity Framework Core model
Sorry for the structure, I know its messed up
I can work, but the better approach would be a converter.
Also, I'd suggest using ETCs (
EntityTypeConfiguration
) rather than attributes. It's a preference, but most seasoned EF devs would agree with that.yeah i personally like ETC/fluent api over attributes, you'll inevitably need to use the fluent api at some point so might as well keep all configuration in the same place
Thanks for the advise. I'll try to rewrite it and use all the suggestion you've provided.
But may I ask, what are the things need to change here?
Oh, and while you get started with ETCs, just to mention something: You don't need to tell EF everything. You only need to specify stuff that doesn't align with conventions or that can be ambiguous.
What do you mean?
In code I've provided. What are the things need to change or need to consider in using ETC. It's kinda new to to me that's why
1- Have your Entity be a pure POCO. No method or fancy properties.
2- In your entity type configuration, just add a converter (use
HasConversion(...)
) to the field.I will consider those. Thank you!!
@Minarii https://learn.microsoft.com/en-us/ef/core/modeling/value-conversions?tabs=data-annotations#composite-value-objects
Look at how they do it:
What if I need to store primitive vales
public List<string> Values
{
get => JsonSerializer.Deserialize<List<string>>(Value);
set => Value = JsonSerializer.Serialize(value);
}
All the Values has a value of string
Same difference
same thing, as far as EF is concerned it's just a complex type that can be converted to and from a string
the converter tells EF how to translate a List<string> or whatever into something that can go into a column and vice versa
And the serializer (whether you're using Newtonsoft's or Microsoft's) will escape what needs to be escaped for proper serialization.
note that if you do this i don't think you'll be able to query into the list like you'd expect in LINQ
Anything that you store serialized should be considered "binary data" as long as it's in the database indeed.
technically since it's json you could write a raw query to do it if the DB supports json, but that's starting to get yucky
i had a similar problem and ended up splitting the collections into tables so they play nice with EF
Yeah, but anything you do through EF wouldn't like it.
Hi everyone, finally the code is now running. Your suggestions are helpful! Thank you very much!
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.