gosukiwi
gosukiwi

Reputation: 1575

Linq to SQL - Refresh changes after failed remove

So this is my code

var r = (from c in db.veClientes where c.Codigo.Equals(cod) select c).FirstOrDefault();
db.veClientes.DeleteOnSubmit(r);
try
{
    db.SubmitChanges();
}
catch
{
    r.DeBaja = 'S';
    db.SubmitChanges();
}

Basically I get a client, try to delete it, if I cant I have to update the client and set a field to 'S'. The problem is the second submitChanges still wants to perform the delete, and fails again, I cannot delete because of relationships, so I just disable the item. How can I refresh the changes so I just update instead of deleting AND updating?

Upvotes: 0

Views: 241

Answers (2)

Siva Charan
Siva Charan

Reputation: 18064

Try this way:-

var r = (from c in db.veClientes where c.Codigo.Equals(cod) select c).FirstOrDefault();
try
{
    r.DeBaja = "S";
    db.SubmitChanges();
}
catch (Exception e)
{
    Console.WriteLine(e);
}

Upvotes: 1

Neil T.
Neil T.

Reputation: 3320

Try this:

try
{
    db.SubmitChanges();
}
catch
{
    db.Refresh(RefreshMode.OverwriteCurrentValues, r);
    r.DeBaja = 'S';
    db.SubmitChanges();
}

Upvotes: 0

Related Questions