2016-08-15 9 views
0

エラー:LINQと自動インクリメント

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.Linq.dll Additional information: Violation of PRIMARY KEY constraint 'PK_users'. Cannot insert duplicate key in object 'dbo.users'. The duplicate key value is (0). The statement has been terminated.

はコード:

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 Managerを使用しています。私のテーブルの主キーは「ID」です。自動インクリメントが有効になります。私はどこにidを設定していないので、それはtechnicaly nullですが、私はこのエラーが表示されます。どんな解決策ですか?

編集:modelclass:

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"); 
       } 
      } 
     } 

とのPropertyChanged:

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(); 
       } 
      } 
     } 
+0

? –

+0

'User'モデルと' InsertOnSubmit'ロジックも表示できますか? –

+0

UserのModelクラスを表示してもよろしいですか? –

答えて

1

あなたが自動生成されるように、あなたのidプロパティをマークする必要があります。これを使用している技術に応じて、デザイナーで変更することができます。属性に[Key]属性を追加するか、流暢な設定を使用してキーとして設定します。 LINQ to SQLデザイナで

、あなたはtrueに自動生成されたプロパティを設定し、OnInsertに自動同期を設定したい - あなたはどのようなORMを使用しているauto generate in LINQ to SQL

+0

そう、私はそれをしました。そして私はDBでもそれをやりました、あるいは私がやったと思いました。私は間違ったコラムでそれをしました: ')私はもう一度チェックして、それは今修正されました、ありがとう。 – user3117628