C
C#11mo ago
\Ali

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
Angius
Angius11mo ago
$code
MODiX
MODiX11mo ago
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/
null
null11mo ago
Did you call .dbContext.SaveChanges() after the remove?
Jimmacle
Jimmacle11mo ago
this also sounds like you're using the same dbcontext for a long time, which is not how you should be using it
\Ali
\AliOP11mo ago
yes
private void dgvUvjerenja_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 5)
{
DialogResult dr = MessageBox.Show("Da li zelite izbrisati ovo uvjerenje?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (dr == DialogResult.Yes)
{
var selectedItem = dgvUvjerenja.SelectedRows[0].DataBoundItem as StudentUvjerenjeIB220319;
dbContext.StudentiUvjerenjaIB220319.Remove(selectedItem);
dbContext.SaveChanges();

UcitajUvjerenja();
}
}
}
private void dgvUvjerenja_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 5)
{
DialogResult dr = MessageBox.Show("Da li zelite izbrisati ovo uvjerenje?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (dr == DialogResult.Yes)
{
var selectedItem = dgvUvjerenja.SelectedRows[0].DataBoundItem as StudentUvjerenjeIB220319;
dbContext.StudentiUvjerenjaIB220319.Remove(selectedItem);
dbContext.SaveChanges();

UcitajUvjerenja();
}
}
}
\Ali
\AliOP11mo ago
No description
\Ali
\AliOP11mo ago
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
Jimmacle
Jimmacle11mo ago
does it show up again in your program when you restart it?
\Ali
\AliOP11mo ago
no
Jimmacle
Jimmacle11mo ago
then you're looking at 2 different databases
\Ali
\AliOP11mo ago
I only have one database
Jimmacle
Jimmacle11mo ago
humor me and make sure that you're opening the exact same file your program is
\Ali
\AliOP11mo ago
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?
Jimmacle
Jimmacle11mo ago
without any restarts/rebuilds?
\Ali
\AliOP11mo ago
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
Jimmacle
Jimmacle11mo ago
does it also show the new record?
\Ali
\AliOP11mo ago
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
Jimmacle
Jimmacle11mo ago
where is dbContext coming from?
\Ali
\AliOP11mo ago
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
Jimmacle
Jimmacle11mo ago
i'm going to guess this is an issue with either a long-lived dbcontext, trying to use untracked entities, or both
\Ali
\AliOP11mo ago
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?
Jimmacle
Jimmacle11mo ago
yes and when is it created and disposed
\Ali
\AliOP11mo ago
its defined in ProjectName.Core, but I made a new instance in the form Im opening its a private field
Jimmacle
Jimmacle11mo ago
you should make a new instance for every operation
\Ali
\AliOP11mo ago
What
Jimmacle
Jimmacle11mo ago
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
\Ali
\AliOP11mo ago
I watched lectures from my uni professors and they add a field, and then use that
Jimmacle
Jimmacle11mo ago
then they're wrong
\Ali
\AliOP11mo ago
lemme try
Jimmacle
Jimmacle11mo ago
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
\Ali
\AliOP11mo ago
Ive made it like this
var dbContext = new DLWMSDbContext();
var selectedItem = dgvUvjerenja.SelectedRows[0].DataBoundItem as StudentUvjerenjeIB220319;
dbContext.StudentiUvjerenjaIB220319.Remove(selectedItem);
dbContext.SaveChanges();

UcitajUvjerenja();
var dbContext = new DLWMSDbContext();
var selectedItem = dgvUvjerenja.SelectedRows[0].DataBoundItem as StudentUvjerenjeIB220319;
dbContext.StudentiUvjerenjaIB220319.Remove(selectedItem);
dbContext.SaveChanges();

UcitajUvjerenja();
and it still isnt working
Jimmacle
Jimmacle11mo ago
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 correctly
\Ali
\AliOP11mo ago
thats what I did before and it still didnt work, so I looked at the solution and the guy did this
var selectedItem = dgvUvjerenja.SelectedRows[0].DataBoundItem as StudentUvjerenjeIB220319;
dbContext.Remove(dbContext.StudentiUvjerenjaIB220319.Single(item => item.Id == selectedItem.Id));
dbContext.SaveChanges();
var selectedItem = dgvUvjerenja.SelectedRows[0].DataBoundItem as StudentUvjerenjeIB220319;
dbContext.Remove(dbContext.StudentiUvjerenjaIB220319.Single(item => item.Id == selectedItem.Id));
dbContext.SaveChanges();
is this what you mean the only way I can get the item is by getting its id from the selected row
Jimmacle
Jimmacle11mo ago
yes, either that or ExecuteDelete to avoid making 2 queries which you don't have to do SaveChanges after because it happens immediately
Angius
Angius11mo ago
.Execute methods my beloved pepelove
\Ali
\AliOP11mo ago
I dont have ExecuteDeleted available as a method like this dbContext.ExecuteDelete
Angius
Angius11mo ago
Seeing how it's a school project, it probably requires an ancient version of .NET Framework, so no surprise there
\Ali
\AliOP11mo ago
its on .net 8 but winforms im running latest version
Angius
Angius11mo ago
EF Core 8 absolutely does have .Execute methods You sure EF version is not outdated maybe?
\Ali
\AliOP11mo ago
No description
Jimmacle
Jimmacle11mo ago
it's on the set, not the context
\Ali
\AliOP11mo ago
its using 7.0.1 for ef
Jimmacle
Jimmacle11mo ago
actually it's not even that, it's on a query
Jimmacle
Jimmacle11mo ago
ExecuteUpdate and ExecuteDelete - EF Core
Using ExecuteUpdate and ExecuteDelete to save changes with Entity Framework Core
\Ali
\AliOP11mo ago
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?
null
null11mo ago
Did you dispose of the dbContext like mentioned here?
\Ali
\AliOP11mo ago
i added that part now and it still does the same bug
\Ali
\AliOP11mo ago
is this disposing properly? i suppose if its in using it implements IDisposable
No description
Angius
Angius11mo ago
No need to save changes after execute methods
\Ali
\AliOP11mo ago
removed it, but it had no effect anyway
Jimmacle
Jimmacle11mo ago
it wouldn't, it's just unnecessary
\Ali
\AliOP11mo ago
i guess my database is just cursed somehow I guess I will close the post because I cant fix it
Jimmacle
Jimmacle11mo ago
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
\Ali
\AliOP11mo ago
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
Jimmacle
Jimmacle11mo ago
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
\Ali
\AliOP11mo ago
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)
Jimmacle
Jimmacle11mo ago
then it's 100% the problem
\Ali
\AliOP11mo ago
Jimmacle
Jimmacle11mo ago
stop using it, just use your application and see if there is still a problem
\Ali
\AliOP11mo ago
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
Jimmacle
Jimmacle11mo ago
then it's an option in the program or he was doing something differently
\Ali
\AliOP11mo ago
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
Jimmacle
Jimmacle11mo ago
sure, but i'm pretty confident it's some concurrency/multi-access issue with your other database client
\Ali
\AliOP11mo ago
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
MODiX
MODiX11mo ago
Jimmacle
then you're looking at 2 different databases
React with ❌ to remove this embed.
Jimmacle
Jimmacle11mo ago
😛
\Ali
\AliOP11mo ago
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
Want results from more Discord servers?
Add your server