【发布时间】:2016-08-15 14:02:04
【问题描述】:
错误:
“System.Data.SqlClient.SqlException”类型的未处理异常 发生在 System.Data.Linq.dll 附加信息:违反 PRIMARY KEY 约束“PK_users”。无法在对象“dbo.users”中插入重复键。这 重复键值为 (0)。 声明已终止。
代码:
private void register_Click(object sender, RoutedEventArgs e)
{
var _user = new user();
if (username.Text.Length == 0)
{
username.Text = "Geef gebruikersnaam in";
}
else if (password.Text.Length == 0)
{
password.Text = "Voer wachtwoord in";
}
else if (password.Text != passcon.Text)
{
password.Text = "Wachtwoorden niet gelijk";
}
else
{
using (MD5 md5Hash = MD5.Create())
{
_user.password = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(password.Text));
}
_user.username = username.Text;
bool contactExists = _dataDC.users.Any(x => x.username.Equals(_user.username));
if (contactExists)
{
MessageBox.Show("Gebruiker bestaat al");
}
else
{
_dataDC.users.InsertOnSubmit(_user);
_dataDC.SubmitChanges();
MessageBox.Show("Registratie succesvol");
MainWindow _main = new MainWindow();
_main.Show();
this.Close();
}
}
}
我正在使用 SQL Server 管理器。我表中的主键是“ID”。启用自动增量。我没有在任何地方设置 id,所以它在技术上是 null,但我得到了这个错误。有什么解决办法吗?
编辑:模型类:
public user user
{
get
{
return this._user.Entity;
}
set
{
user previousValue = this._user.Entity;
if (((previousValue != value)
|| (this._user.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
this._user.Entity = null;
previousValue.recipients.Remove(this);
}
this._user.Entity = value;
if ((value != null))
{
value.recipients.Add(this);
this._recipient1 = value.username;
}
else
{
this._recipient1 = default(string);
}
this.SendPropertyChanged("user");
}
}
}
属性改变了:
public partial class user : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private int _userid;
private string _username;
private System.Data.Linq.Binary _password;
private EntitySet<email> _emails;
private EntitySet<recipient> _recipients;
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnuseridChanging(int value);
partial void OnuseridChanged();
partial void OnusernameChanging(string value);
partial void OnusernameChanged();
partial void OnpasswordChanging(System.Data.Linq.Binary value);
partial void OnpasswordChanged();
#endregion
public user()
{
this._emails = new EntitySet<email>(new Action<email>(this.attach_emails), new Action<email>(this.detach_emails));
this._recipients = new EntitySet<recipient>(new Action<recipient>(this.attach_recipients), new Action<recipient>(this.detach_recipients));
OnCreated();
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_userid", DbType="Int NOT NULL", IsPrimaryKey=true, IsDbGenerated=true)]
public int userid
{
get
{
return this._userid;
}
set
{
if ((this._userid != value))
{
this.OnuseridChanging(value);
this.SendPropertyChanging();
this._userid = value;
this.SendPropertyChanged("userid");
this.OnuseridChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_username", DbType="VarChar(50) NOT NULL", CanBeNull=false)]
public string username
{
get
{
return this._username;
}
set
{
if ((this._username != value))
{
this.OnusernameChanging(value);
this.SendPropertyChanging();
this._username = value;
this.SendPropertyChanged("username");
this.OnusernameChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_password", DbType="VarBinary(MAX) NOT NULL", CanBeNull=false, UpdateCheck=UpdateCheck.Never)]
public System.Data.Linq.Binary password
{
get
{
return this._password;
}
set
{
if ((this._password != value))
{
this.OnpasswordChanging(value);
this.SendPropertyChanging();
this._password = value;
this.SendPropertyChanged("password");
this.OnpasswordChanged();
}
}
}
【问题讨论】:
-
你用的是什么ORM?
-
您能否展示
user模型以及您的InsertOnSubmit逻辑? -
你能展示一下User的Model类吗?
-
您应该使用比 MD5 更强大(且速度更慢)的哈希函数作为密码,并包含盐以帮助防止您的哈希密码在泄露时从彩虹表中轻易猜到。见crackstation.net/hashing-security.htm
-
@tvanfosson 我知道,我刚刚开始,无意发布此内容。我只是拿了我能找到的第一个:)
标签: c# sql-server linq