DuplicateKeyException在LINQ,但我已经设置自动递增和自动同步 [英] DuplicateKeyException in LINQ, but I've set auto increment and auto sync
问题描述
我得到在我的C#code进行DuplicateKeyException错误。
我已经设置了自动生成= true,并且自动同步= OnInsert在我的dbml。我还没有碰到PK场中的任何手写code(如下图所示[我的主键字段实际上被称为PK])。
使用(DeviceExerciseDataDataContext上下文=新DeviceExerciseDataDataContext())
{
的foreach(数据tgudData的数据。数据)
{
TGD =新tableData();
tgd.FK =键;
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();
}
这是在code在设计列(列被命名为PK和FK)
[全球:: System.Data.Linq.Mapping.ColumnAttribute(存储=_ PK,自动同步= AutoSync.OnInsert,的DbType =诠释NOT NULL,IsPrimaryKey = TRUE, IsDbGenerated =真)
公众诠释PK
{
得到
{
返回this._PK;
}
组
{
如果((this._PK!=值))
{
this.OnPKChanging(值);
this.SendPropertyChanging();
this._PK =价值;
this.SendPropertyChanged(PK);
this.OnPKChanged();
}
}
}
[全球:: System.Data.Linq.Mapping.ColumnAttribute(存储=_ FK的DbType =智力)
公共System.Nullable< INT> FK
{
得到
{
返回this._FK;
}
组
{
如果((this._FK!=值))
{
this.OnFKChanging(值);
this.SendPropertyChanging();
this._FK =价值;
this.SendPropertyChanged(FK);
this.OnFKChanged();
}
}
}
看着你自动生成的code,你可以尝试改变在设计服务器数据类型为诠释NOT NULL的IDENTITY 。我不怀疑这是要修复它,但你永远不知道。
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();
}
}
}
Looking at your auto-generated code, you may try changing the server data type in the designer to Int NOT NULL IDENTITY. I don't suspect this is going to fix it but you never know.
这篇关于DuplicateKeyException在LINQ,但我已经设置自动递增和自动同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!