简体   繁体   中英

SubmitChanges not updating

First of all I would like to precise that I read all the related subjects about SubmitChanges issues but couldn't find anything solving my problem... I have several tables which are working perfectly fine using the same way (excepting composite primary keys) but this one doesn't, I just can't understand why. Here's the table :

CREATE TABLE [dbo].[Trainings] (
    [playerId] INT NOT NULL,
    [stepId]  INT NOT NULL,
    [value]   INT NOT NULL,
    CONSTRAINT [PK_Trainings] PRIMARY KEY CLUSTERED ([playerId] ASC, [stepId] ASC)
);

The related class :

[Table(Name = "Trainings")]
public class Training
{
    [Column(IsPrimaryKey = true)] public int PlayerID { get; set; }
    [Column(IsPrimaryKey = true, Name = "stepId")] public int Step { get; set; }
    [Column] public int Value { get; set; }

    public Training(int playerId, int step, int value)
    {
        PlayerID = playerId;
        Step = step;
        Value = value;
    }
}

And the database update call :

public class Database : DataContext
{
    public Table<Training> Trainings;

    public Training GetTraining(int playerId, int step)
    {
        Training training = Trainings.Where(t => t.PlayerID == playerId && t.Step == step).Single();
        return training;
    }

    public void UpdateTraining(Training training)
    {
        Training DBtraining = GetTraining(training.PlayerID, training.Step);
        DBtraining = training;
        SubmitChanges();
    }
}

I tried to remove the composite primary keys and add an only "id" key but the table is not updated anyway. The only way I found to make it work is to delete the old row and add a new one with the new value, but it is obviously horrible.

When I tried to put some break point in it, I got this error message which doesn't show on normal mode :

The operation cannot be performed during a call to SubmitChanges.

EDIT:

Seems like the problem is due to an SQL Exception. Here's the exception tracktrace :

System.Exception.StackTrace.get
System.Data.Linq.DataContext.CheckNotInSubmitChanges()
System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
System.Data.Linq.DataContext.SubmitChanges()
Database.UpdateTraining(Training training)

You are not making any changes. This line of code will only assign your local variable DBtraining a new value:

DBtraining = training;

What you probably want to do is this:

DBtraining.Value = training.Value

That will update the Value of the current instance in the database. This will then be saved when you call SubmitChanges .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM