dbContext.Remove removes from application, but in database it remains
I click the delete button, it gets removed from the program. I check in sqlite db browser and the record is still there? This persists through rebuilds and reruns, in the program it doesn't show, in the db browser it does. This is a WinForms project btw
68 Replies
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/Did you call .dbContext.SaveChanges() after the remove?
this also sounds like you're using the same dbcontext for a long time, which is not how you should be using it
yes
It gets removed from my program, but in the db browser even after I refresh and restart the program it doesnt want to go away somehow
does it show up again in your program when you restart it?
no
then you're looking at 2 different databases
I only have one database
humor me and make sure that you're opening the exact same file your program is
If I add a record manually, it shows up correctly
for example I add a record in the db and open the corresponding Student, it will show me the entry, so its the right database?
without any restarts/rebuilds?
yes
actually nvm it doesnt live update
even tho i run a query on each time the new form is opened
more info: I added a new record to that table, and on rerunning my program, it shows the record i deleted previously
does it also show the new record?
after I rerun the program, it shows the new record I added and the old one which was hidden when I clicked delete
so 2 records, and 2 in the database
where is
dbContext
coming from?I have a project which is ProjectName.Core and ProjectName.WinForms, and the db is in the root folder of ProjectName.WinForms, I dont know if that should be interferring
i'm going to guess this is an issue with either a long-lived dbcontext, trying to use untracked entities, or both
because this is part of an exam task and the way the files are strucutred including the database is something I cant change
you mean where is it defined or?
yes
and when is it created and disposed
its defined in ProjectName.Core, but I made a new instance in the form Im opening
its a private field
you should make a new instance for every operation
What
they aren't designed to stick around for a long time
so for example here, you should be creating a new dbcontext in your event handler and disposing it
I watched lectures from my uni professors and they add a field, and then use that
then they're wrong
lemme try
it can work but it's not how it was designed to be used and can cause desynchronization and change tracking issues
maybe i'm jaded, but always take your school's examples and "best practices" with a grain of salt
they're usually not the best
Ive made it like this
and it still isnt working
you shouldn't use the class that's in your data grid or whatever that is, you need to get it from the database again by ID or just
ExecuteDelete
it
the only entities that you should ever pass into a dbcontext should be entities returned by a query to that dbcontext (or brand new ones that you're adding)
also, you need to put the dbcontext in a using so it's disposed correctlythats what I did before and it still didnt work, so I looked at the solution and the guy did this
is this what you mean
the only way I can get the item is by getting its id from the selected row
yes, either that or
ExecuteDelete
to avoid making 2 queries
which you don't have to do SaveChanges after because it happens immediately.Execute
methods my beloved I dont have ExecuteDeleted available as a method like this
dbContext.ExecuteDelete
Seeing how it's a school project, it probably requires an ancient version of .NET Framework, so no surprise there
its on .net 8 but winforms
im running latest version
EF Core 8 absolutely does have
.Execute
methods
You sure EF version is not outdated maybe?it's on the set, not the context
its using 7.0.1
for ef
actually it's not even that, it's on a query
ExecuteUpdate and ExecuteDelete - EF Core
Using ExecuteUpdate and ExecuteDelete to save changes with Entity Framework Core
alr modified it let me try now
it still deletes them in the program but they remain in the database until I add another record, then they all show
dbContext.StudentiUvjerenjaIB220319.Where(item => item.Id == selectedItem.Id).ExecuteDelete();
I did it like this, it's correct right?Did you dispose of the dbContext like mentioned here?
i added that part now and it still does the same bug
is this disposing properly? i suppose if its in using it implements IDisposable
No need to save changes after execute methods
removed it, but it had no effect anyway
it wouldn't, it's just unnecessary
i guess my database is just cursed somehow
I guess I will close the post because I cant fix it
or leave it open to see if other people have ideas?
kinda running out though, this isn't a complicated query so i doubt it's the problem
i just checked I tried changing the state of the checkbox
and i did the Entry modified state thing
and saved changes
in the app it keeps saying that I clicked the buttona
and its persistent through different runs
but on the database it wasnt affected
so this isnt an issue around the query at all, something is wrong with the database
but we get this on an exam, I dont know how is it wrongly configured
okay, if it's persistent through different runs then where do you think it's being persisted if not the database?
i'm like 90% sure this is a problem with the db viewer you're using at this point
thats the problem, I dont know where is it persistent, and the funny thing is when I add a new record through the db viewer, and rerun my program, it removes the changes (clicking on the checkbox for example, or brings back "deleted" items)
then it's 100% the problem
this is the db viewer https://sqlitebrowser.org/
stop using it, just use your application and see if there is still a problem
i must be doing something wrong, because in my lectures the professor used it without issues
and it updated in real time with the changes in the program
then it's an option in the program or he was doing something differently
ive downloaded this project from github, and my winforms is .net8 while my core project library has .net6 with EF version 7.0.1 instead of 8.0.1 with .net8
im gonna try updating to the latest and changing the project settings to net8 and see if it works
sure, but i'm pretty confident it's some concurrency/multi-access issue with your other database client
Fixed it
It was none of the above @jIMMACLE, I was looking in the DB browser on the file ./file.db instead of bin/Debug/file.db
couldve figured this out sooner, because the app and the database were not in sync
i guess I learned the hard way
remember when i said this? https://discord.com/channels/143867839282020352/1194376684607119391/1194378791368282243
Jimmacle
then you're looking at 2 different databases
Quoted by
<@901546879530172498> from #dbContext.Remove removes from application, but in database it remains (click here)
React with ❌ to remove this embed.
😛
yeah but at that time i was hotheaded i didnt realize it was being copied over to debug
in the exam template for the db its set to copy whenever a change has been made
shouldve looked at that sooner