DuplicateKeyException in LINQ, but I've set auto increment and auto sync

Posted by Fritos on Stack Overflow See other posts from Stack Overflow or by Fritos
Published on 2011-06-24T14:33:15Z Indexed on 2011/06/24 16:22 UTC
Read the original article Hit count: 243

Filed under:
|
|

I'm getting a DuplicateKeyException error in my C# code.

I've set Auto Generated = true, and Auto-Sync = OnInsert in my dbml. I'm not even touching the PK field in any manually written code (as seen below [My primary key field is actually called PK]).

using (DeviceExerciseDataDataContext context = new DeviceExerciseDataDataContext())
                {
 foreach(Data tgudData in data.Data)
                {
                    tgd = new tableData();
                    tgd.FK = key;
                    tgd.Time = tgudData.TimeStamp;
                    tgd.Calories = Convert.ToInt32(tgudData.Calories);
                    tgd.HeartRate = tgudData.AvgHr;
                    tgd.BenchAngle = tgudData.Angle;
                    tgd.WorkoutTarget = 0;
                    tgd.Reps = tgudData.Reps;
                    context.tableDatas.InsertOnSubmit(tgd);
                }
                context.SubmitChanges();
}

This is the code for the column in the designer (columns are named PK and FK)

[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_PK", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL", IsPrimaryKey=true, IsDbGenerated=true)]
        public int PK
        {
            get
            {
                return this._PK;
            }
            set
            {
                if ((this._PK != value))
                {
                    this.OnPKChanging(value);
                    this.SendPropertyChanging();
                    this._PK = value;
                    this.SendPropertyChanged("PK");
                    this.OnPKChanged();
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_FK", DbType="Int")]
        public System.Nullable<int> FK
        {
            get
            {
                return this._FK;
            }
            set
            {
                if ((this._FK != value))
                {
                    this.OnFKChanging(value);
                    this.SendPropertyChanging();
                    this._FK = value;
                    this.SendPropertyChanged("FK");
                    this.OnFKChanged();
                }
            }
        }

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET