1

I have a database table and I'm trying to update a cell with a money datatype (decimal) and I'm using LINQ and and Entity Framework.

Unfortunately Context.SaveChanges(); won't work for some reason.

Here is the table structure

Type Amount Machine
Cash 1000   Tablet

Here is my code:

using (var GC = new GroundCommanderEntities())
{
    PAYMENT_Repo PAYMENTREPO = new PAYMENT_Repo(); 
    var ExistingCashPayment = GC.PAYMENT_Repo
        .Where(Filter => Filter.Type == "Cash" && Filter.Machine == "Tablet").ToList();

    string type = "";
    var amt = 0.00m;

    foreach (var item in ExistingCashPayment)
    {
        type = item.Type;
        amt = item.Amount;
    }

    if (type == cbPaymentType.Text) //a combobox that contains Types 
    {
        PAYMENTREPO.Amount = amt + Convert.ToDecimal(txtTendering.Text);                    
        GC.SaveChanges();

        return true;
     }
     else
     {
         return false;
     }             
}
6
  • Do you get an error? Commented Jul 22, 2015 at 8:00
  • @MickyDuncan No it doesn't. When I debug, step into GC.SaveChanges(); and look into the table, it didn't apply the change. Commented Jul 22, 2015 at 8:02
  • You do know in your foreach you keep writing over the old values of amt and type? Is that the problem (Before anyone comments I know strings are immutable) Commented Jul 22, 2015 at 8:04
  • @JamieRees I have a foreach loop because I have the ExistingCashPayment in a List. I can't go through the if statement if the table is null. It is looking for an existing row with Cash type in it. What can you suggest? Commented Jul 22, 2015 at 8:08
  • 1
    What I mean is you keep overwriting the value of amt and type everytime in the loop. Really you should make amt and type a collection. Commented Jul 22, 2015 at 8:09

3 Answers 3

2

It looks like GC is your DbContext. If so you need to add the state of the object is modified as follows...

db.Entry(obj).State = System.Data.Entity.EntityState.Modified; db.SaveChanges();

This should work.

Sign up to request clarification or add additional context in comments.

Comments

1

There are a couple of problems here:

Most importantly, PAYMENTREPO is a new instance - its not from the db context, so changing values on it has no effect. You need to add it before saving changes:

PAYMENTREPO.Amount = amt + Convert.ToDecimal(txtTendering.Text);                    
GC.PAYMENTREPOs.Add(PAYMENTREPO);
GC.SaveChanges();

Second, which won't cause the complete fail to update, but will cause the code to do things other than you expect:

foreach (var item in ExistingCashPayment)
{
    type = item.Type;
    amt = item.Amount;
}

This loop will leave type and amt set to the last value in the ExistingCashPayment list, rather than some sort of accumulation of the contents of that list, which would be the desired behaviour implied by having the loop there in the first place.

Comments

1

When you create a new entity like you do, you must add it to the context before calling SaveChanges():

using (var GC = new GroundCommanderEntities())
{
    PAYMENT_Repo PAYMENTREPO = new PAYMENT_Repo(); 
    var ExistingCashPayment = GC.PAYMENT_Repo
        .Where(Filter => Filter.Type == "Cash" && Filter.Machine == "Tablet").ToList();

    string type = "";
    var amt = 0.00m;

    foreach (var item in ExistingCashPayment)
    {
        type = item.Type;
        amt = item.Amount;
    }

    if (type == cbPaymentType.Text) //a combobox that contains Types 
    {
        PAYMENTREPO.Amount = amt + Convert.ToDecimal(txtTendering.Text);                    

        // add your NEW entity to the context!
        // depending on which exact version of EF you're using, this
        // might be called `.Add()` or `.AddObject()`
        // Also, the name "PAYMENTREPOs" is just a guess - it might be 
        // different in your concrete case - adapt as needed!
        GC.PAYMENTREPOs.Add(PAYMENTREPO);

        GC.SaveChanges();

        return true;
     }
     else
     {
         return false;
     }             
}

2 Comments

I have another function that does the adding. to the PAYMENT_Repo table. I need not to add another row but rather add the Amount when it is existing in the table.
@JoshuaMasangcay: in that case, you must load (fetch) the existing row from the context first - before updating it! And then call .SaveChanges() after the changes are applied

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.