My controller:
[HttpPost]
public ActionResult Deposit(DepositTicket dt)
{
using (var db = new MatchGamingEntities())
{
MembershipUser currentUser = Membership.GetUser();
Guid UserId = (Guid)currentUser.ProviderUserKey;
var MyAccount = from a in db.Accounts
where a.UserId == UserId
select new Account{
AccountId = a.AccountId,
Balance = a.Balance
};
BankTransaction transaction = new BankTransaction();
transaction.Amount = dt.Amount;
transaction.AccountId = MyAccount.SingleOrDefault().AccountId;
transaction.Created = DateTime.Today;
transaction.TransactionType = "Credit";
Debug.Write("Amount: " + transaction.Amount + " AccountId " + transaction.AccountId);
db.BankTransactions.AddObject(transaction);
MyAccount.SingleOrDefault().Balance += transaction.Amount;
//Update Query
db.SaveChanges();
return View();
}
Where the comment “Update Query” is, is where I want to add an update query to updated the Account table with an Account object. I want to update the exisiting record, can this be done using the predefined functions for db.Accounts or would I have to write a linq query?
It seems like you are going to an awful lot of trouble to get a custom account object to then just want to update the original account record. I would suggest just working with the original account object and make the necessary updates, then when SaveChanges is called it will update your account record.
Here are my suggested changes
Hope this helps.