Am I doing this correctly? (Foreign Keys & Such, Entity Framework & SQL Server)
I'm trying to figure out how to store objects within objects in the database, that can only be accessed directly or through the UserModel... Kinda like having a sub-folder for organization sake so the usermodel doesn't have 30 columns in it.
I only understand sql on a basic beginner level (one table for one thing, through entity framework only) and asking chatgpt has gotten me this far and i'm not srue if im even doing it right.
21 Replies
"meow" and "rawr".
random words influenced by my cat on my desk lol
You could Also add it as a JSON string into the db
I think you can serialize whole objects in vanilla C#
What database is that
sql server
Ah
Honestly I don't think you can easily store sub objects without creating all the values
My approach for this would be to have the basic things and for things like object extensions I'll serialize them into a JSON object and store them in some additional attribute
Also I'd say you should use the UNIX timestamp instead of ISO datetime, just preference tho since it's easier to perform math on it
is unix time generally the way to go in most apps compared to utc?
the thing is that UNIX timestamp is the exact amount of seconds since 1970 (i think) so you can do very precise time measures
and also doing things like subscriptions you can just have your subscription age in seconds and then compare if the current timestamp > (subscriptionStart + subscriptionAge)
(this is just an example of the things you can do with it but its very useful to work with the UNIX format instead)
Btw I cannot modify the refresh token string through the usermodel like this api in the original code.
i usually use stuff like MySQL and SQLite
also i dont think you need to tell the async Task what to return
i usually declare my functions as IActionResult and it works fine
like this
This will work only because
async
here is useless as you're not awaiting anything
Async methods have to return a task
As for your initial question... it's not how I would've done it, but it should be fineI do have some worries about using a json. While im using a test project here in this help thread (for learning purposes and testing how it works), my intention in the real project im working on is to save user game data, which includes player inventories which consist of lists of item objects and i'm worried about database performance, api performance of converting jsons every http request, or various string limits. These strings could be 4-5 digits worth of chars so idk if a json string is enough to handle it.
I could probably impose inventory size limits (ex: 99 items) but the string could still be thousands of chars long.
Was thinking maybe separate database tables would help with this.
SQL should have types like TEXT and LONGTEXT, which can store 65.5k characters, or In the case of LONGTEXT it can store up to 4GB
You could Also use a blob but I'm not sure if that's the kinda approach you'd want
no clue what a blob is 😄
AFAIK it's just bytes in the db
But I'm not sure I've never worked with them
Even SQLite supports dedicated JSON columns nowadays
So if you want to store JSON in a relational db, use that
Everything is varchar.
Except it's JSONB, binary JSON, so in case of Postgres at least you can query it and stuff
IIRC you can even have FKs in the json
Speaking of SQL columns, does it make any difference in performance / memory for the different types
Like TEXT and LONGTEXT or INT and BIGINT