SQLITE EFcore saving value
Hi, i'm working on a project which have a SQLITE database. I'm using EFcore to managed it. I want to know if there is a way to automatically add/remove/update by tracking or idk all the variable in my list Widths.
Basically i want to call a methods
Save
that will add thickness if not added or add/remove/update elements in width a if thickness i already in the database.
38 Replies
Nothing is persisted in your database until you call
context.SaveChangesAsync
(or the non-async version), but otherwise EF already does this, if I have understood your question correctly
ie, if you do
it will associate that width with that thickness
if you modify any item in thickness.Widths
before calling SaveChanges, it will also be updated accordingly.i wanted more something like this :
What "stuff in widths"?
all the variable in my list widths
A width has:
1. An ID, which gets generated automatically anyway
2. FK to a width, which will be set using Pobiega's method
3. A value that you want to give some default value to, I guess?
is what you want?
not exactly because i allready use that king of method i will explain how my app is stuctured
Or
?
in fact my app load all the database, then i do my stuff like adding removing or even updating Width after that i want to call
Save
that will automattically track all the new item or removed items in WidthsEF tracks all additions, changes, and deletions
And
await _context.SaveChangesAsync();
persists themi need to use
_context.Thicknesses.Add(thickness);
if i want him to track this right ? because i i do this thicknesses.Add(thickness);
then save async it won't know that i have change stuff ?Depends what is
thicknesses
here is how i load my
thicknesses
:
nope
thats a filtered copy of your thicknesses
What's this formatting 💀
First time I see someone place dots at the end of line lol
you need to add it to the
DbSet
if its a new entity to be trackedAnd, yeah. Each individual thickness will be tracked, but the list as a whole is not a reflection of the dbset
also this. Please put the . on the new line :d
oh and the context is disposed after the list is fetched
i will i promise i will search how to do it ahah
Just... do
Instead of
write
Or use $prettycode shortcuts
To format your code in Visual Studio, Visual Studio Code, Rider, use the following shortcut:
NOTE: the first key must be held while doing it.
https://cdn.discordapp.com/attachments/569261465463160900/899513918567890944/2021-10-18_01-26-35.gif
this copy the list to right ?
yeah this isnt a fix to your problem
it was just an example of proper formatting
oh ok i will correct that i'm using codderush to format all king of stuff i'm looking to it
your problem is fixed by having a database context available when you plan to insert data into your database 😛
so i should keep a database context open the moment i load my items ?
$itdepends
Impossible to answer
You need a context available at the time where you insert new data into your database
thats a hard fact
should it be the same context? Depends on how your application is made.
so if i'm copying the thickness, in fact it keep the id right ? can i track it when opening the context and then saving it ? EFcore will be able to add/remove/update all the item in thickness no ?
yeah, anything you fetch from the context will have the correct IDs
however, if you fetch it in one context, then try to save it on another context, you will need to let the new context know about the item
as tracking is per-context
ok so to achieve my goal i will have to create a save function that will track all the change made in order to update
thickness
and widths
in my db"track all the changes" is a bit much
just add the
thickness
to the trackerok i think i've understand i'll try some test by my side, if i'm right by tracking thickness it should also track the width in it and i will be able to save all the changes made in it like all the removed/added width item
there it is i've updated my formatting 😉 :
I'd argue your data has a weird shape for a relational database
what do you mean by weird shape ?
i've tried to dissociate the app from the database to be able to modify my item freely without breaking my db with wrong manipulation. And if i'm done to update/add/remove item then i call save to interact with the db and confirm all the changes made.