C
C#11mo ago
Antonio

Can't withdraw and save new balance to database

Hey can someone help me with my issues I've been stuck on for hours? I am trying to make a withdraw from an account and from there update its new balance to the database. But every time I click withdraw I get 0 on the new balance and the database is not updating. Can someone look into my code and see if you can find the problem? Look into the Withdraw and AccountManagement. Thats were my issuses are. https://github.com/agaquit/WebBankApplication
GitHub
GitHub - agaquit/WebBankApplication
Contribute to agaquit/WebBankApplication development by creating an account on GitHub.
5 Replies
Pobiega
Pobiega11mo ago
namespace WebBankApplication.Data
{
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
}
namespace WebBankApplication.Data
{
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
}
? You have an empty context. Oh you have two database contexts.
Antonio
AntonioOP11mo ago
Ye, the one database I am trying to update is the BankApplicationData that I scaffolded
Pobiega
Pobiega11mo ago
Your code structure makes this project very annoying to navigate btw have you tried just setting breakpoints and stepping through the withdrawal flow?
public IActionResult OnPost()
{
var accountDb = _accountService.GetAccount(AccountId); // Use AccountId here
if (accountDb.Balance < Amount)
{
ModelState.AddModelError("Amount", "You don't have that much money!");
}

if (ModelState.IsValid)
{
accountDb.Balance -= Amount;
_accountService.Update(accountDb);
}
return Page();


}
public IActionResult OnPost()
{
var accountDb = _accountService.GetAccount(AccountId); // Use AccountId here
if (accountDb.Balance < Amount)
{
ModelState.AddModelError("Amount", "You don't have that much money!");
}

if (ModelState.IsValid)
{
accountDb.Balance -= Amount;
_accountService.Update(accountDb);
}
return Page();


}
this has... issues if there isnt enough money, you set a model error, then proceed to reduce the balance anyway :p also, you are using EF Core wrong
public Account GetAccount(int accountId)
{
return _dbContext.Accounts.First(a => a.AccountId == accountId);
}

public void Update(Account account)
{
_dbContext.Accounts.Update(account);
_dbContext.SaveChanges();
}
public Account GetAccount(int accountId)
{
return _dbContext.Accounts.First(a => a.AccountId == accountId);
}

public void Update(Account account)
{
_dbContext.Accounts.Update(account);
_dbContext.SaveChanges();
}
why are these not async btw thats a huge problem on its own but even ignoring that, when you fetch an object from EF, EF "tracks" that object
Angius
Angius11mo ago
WHy multiple contexts tho
Pobiega
Pobiega11mo ago
edits to that object will be saved to database when next you call SaveChangesAsync without having to call Update you even have a Withdraw method in your account service, but opt to not use it something about scaffold vs code first this project is in general in very poor shape
Want results from more Discord servers?
Add your server