0

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)
Flash_Back
  • 565
  • 3
  • 8
  • 31

1 Answers1

0

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.

Mårten Wikström
  • 11,074
  • 5
  • 47
  • 87
  • Thanks for your answer. This line works fine for the other tables but you are right, I changed it and got the error message in the first post: "The operation cannot be performed during a call to SubmitChanges." – Flash_Back Mar 23 '14 at 16:39
  • What's the callstack of that exception? It sounds like that is another problem, and that it should probably be asked as another question. – Mårten Wikström Mar 23 '14 at 16:45
  • Thanks I edited the first post, hoping that this is what you wanted to see. If not please let me now, I'll edit again. – Flash_Back Mar 23 '14 at 17:02