У меня есть база данных с этими таблицами:
PERSON ==> id=>int_primarykey(asc) name=>nchar(20) town=>nchar(20)
EMAIL ==> id=>primarykey(asc) idperson=>int mailaddress=>nchar(20)
telephone ==> id=>primarykey(asc) idperson=>int telnumber=>nchar(20)
Итак, я добавил в форму таблицу данных с навигатором привязки.Проблема в том, что когда я пытаюсь добавить новую запись, в таблице данных у меня всегда есть id = 0, а когда я пытаюсь сохранить новую запись, я возвращаю duplicatekeyexception, потому что этот идентификатор уже существует.реализовать автоинкремент для id?Вот этот класс:
#pragma warning disable 1591
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.1
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace Rubrica
{
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Linq.Expressions;
using System.ComponentModel;
using System;
[global::System.Data.Linq.Mapping.DatabaseAttribute(Name="rubrica")]
public partial class DataClasses1DataContext : System.Data.Linq.DataContext
{
private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
#region Extensibility Method Definitions
partial void OnCreated();
partial void Insertemail(email instance);
partial void Updateemail(email instance);
partial void Deleteemail(email instance);
partial void Insertpersona(persona instance);
partial void Updatepersona(persona instance);
partial void Deletepersona(persona instance);
partial void Inserttelefono(telefono instance);
partial void Updatetelefono(telefono instance);
partial void Deletetelefono(telefono instance);
#endregion
public DataClasses1DataContext() :
base(global::Rubrica.Properties.Settings.Default.rubricaConnectionString, mappingSource)
{
OnCreated();
}
public DataClasses1DataContext(string connection) :
base(connection, mappingSource)
{
OnCreated();
}
public DataClasses1DataContext(System.Data.IDbConnection connection) :
base(connection, mappingSource)
{
OnCreated();
}
public DataClasses1DataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public DataClasses1DataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) :
base(connection, mappingSource)
{
OnCreated();
}
public System.Data.Linq.Table<email> emails
{
get
{
return this.GetTable<email>();
}
}
public System.Data.Linq.Table<persona> personas
{
get
{
return this.GetTable<persona>();
}
}
public System.Data.Linq.Table<telefono> telefonos
{
get
{
return this.GetTable<telefono>();
}
}
}
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.email")]
public partial class email : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private int _idpersona;
private string _mail;
private int _id;
private EntityRef<persona> _persona;
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnidpersonaChanging(int value);
partial void OnidpersonaChanged();
partial void OnmailChanging(string value);
partial void OnmailChanged();
partial void OnidChanging(int value);
partial void OnidChanged();
#endregion
public email()
{
this._persona = default(EntityRef<persona>);
OnCreated();
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_idpersona", DbType="Int NOT NULL")]
public int idpersona
{
get
{
return this._idpersona;
}
set
{
if ((this._idpersona != value))
{
if (this._persona.HasLoadedOrAssignedValue)
{
throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
}
this.OnidpersonaChanging(value);
this.SendPropertyChanging();
this._idpersona = value;
this.SendPropertyChanged("idpersona");
this.OnidpersonaChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="email", Storage="_mail", DbType="NChar(20) NOT NULL", CanBeNull=false)]
public string mail
{
get
{
return this._mail;
}
set
{
if ((this._mail != value))
{
this.OnmailChanging(value);
this.SendPropertyChanging();
this._mail = value;
this.SendPropertyChanged("mail");
this.OnmailChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_id", DbType="Int NOT NULL", IsPrimaryKey=true)]
public int id
{
get
{
return this._id;
}
set
{
if ((this._id != value))
{
this.OnidChanging(value);
this.SendPropertyChanging();
this._id = value;
this.SendPropertyChanged("id");
this.OnidChanged();
}
}
}
[global::System.Data.Linq.Mapping.AssociationAttribute(Name="persona_email", Storage="_persona", ThisKey="idpersona", OtherKey="id", IsForeignKey=true)]
public persona persona
{
get
{
return this._persona.Entity;
}
set
{
persona previousValue = this._persona.Entity;
if (((previousValue != value)
|| (this._persona.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
this._persona.Entity = null;
previousValue.emails.Remove(this);
}
this._persona.Entity = value;
if ((value != null))
{
value.emails.Add(this);
this._idpersona = value.id;
}
else
{
this._idpersona = default(int);
}
this.SendPropertyChanged("persona");
}
}
}
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.persona")]
public partial class persona : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private int _id;
private string _nome;
private string _cognome;
private string _indirizzo;
private EntitySet<email> _emails;
private EntitySet<telefono> _telefonos;
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnidChanging(int value);
partial void OnidChanged();
partial void OnnomeChanging(string value);
partial void OnnomeChanged();
partial void OncognomeChanging(string value);
partial void OncognomeChanged();
partial void OnindirizzoChanging(string value);
partial void OnindirizzoChanged();
#endregion
public persona()
{
this._emails = new EntitySet<email>(new Action<email>(this.attach_emails), new Action<email>(this.detach_emails));
this._telefonos = new EntitySet<telefono>(new Action<telefono>(this.attach_telefonos), new Action<telefono>(this.detach_telefonos));
OnCreated();
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_id", DbType="Int NOT NULL", IsPrimaryKey=true)]
public int id
{
get
{
return this._id;
}
set
{
if ((this._id != value))
{
this.OnidChanging(value);
this.SendPropertyChanging();
this._id = value;
this.SendPropertyChanged("id");
this.OnidChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_nome", DbType="NChar(10) NOT NULL", CanBeNull=false)]
public string nome
{
get
{
return this._nome;
}
set
{
if ((this._nome != value))
{
this.OnnomeChanging(value);
this.SendPropertyChanging();
this._nome = value;
this.SendPropertyChanged("nome");
this.OnnomeChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_cognome", DbType="NChar(15) NOT NULL", CanBeNull=false)]
public string cognome
{
get
{
return this._cognome;
}
set
{
if ((this._cognome != value))
{
this.OncognomeChanging(value);
this.SendPropertyChanging();
this._cognome = value;
this.SendPropertyChanged("cognome");
this.OncognomeChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_indirizzo", DbType="NChar(50)")]
public string indirizzo
{
get
{
return this._indirizzo;
}
set
{
if ((this._indirizzo != value))
{
this.OnindirizzoChanging(value);
this.SendPropertyChanging();
this._indirizzo = value;
this.SendPropertyChanged("indirizzo");
this.OnindirizzoChanged();
}
}
}
[global::System.Data.Linq.Mapping.AssociationAttribute(Name="persona_email", Storage="_emails", ThisKey="id", OtherKey="idpersona")]
public EntitySet<email> emails
{
get
{
return this._emails;
}
set
{
this._emails.Assign(value);
}
}
[global::System.Data.Linq.Mapping.AssociationAttribute(Name="persona_telefono", Storage="_telefonos", ThisKey="id", OtherKey="idpersona")]
public EntitySet<telefono> telefonos
{
get
{
return this._telefonos;
}
set
{
this._telefonos.Assign(value);
}
}
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private void attach_emails(email entity)
{
this.SendPropertyChanging();
entity.persona = this;
}
private void detach_emails(email entity)
{
this.SendPropertyChanging();
entity.persona = null;
}
private void attach_telefonos(telefono entity)
{
this.SendPropertyChanging();
entity.persona = this;
}
private void detach_telefonos(telefono entity)
{
this.SendPropertyChanging();
entity.persona = null;
}
}
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.telefono")]
public partial class telefono : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private int _id;
private string _numero;
private int _idpersona;
private EntityRef<persona> _persona;
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnidChanging(int value);
partial void OnidChanged();
partial void OnnumeroChanging(string value);
partial void OnnumeroChanged();
partial void OnidpersonaChanging(int value);
partial void OnidpersonaChanged();
#endregion
public telefono()
{
this._persona = default(EntityRef<persona>);
OnCreated();
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_id", DbType="Int NOT NULL", IsPrimaryKey=true)]
public int id
{
get
{
return this._id;
}
set
{
if ((this._id != value))
{
this.OnidChanging(value);
this.SendPropertyChanging();
this._id = value;
this.SendPropertyChanged("id");
this.OnidChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="telefono", Storage="_numero", DbType="NChar(20) NOT NULL", CanBeNull=false)]
public string numero
{
get
{
return this._numero;
}
set
{
if ((this._numero != value))
{
this.OnnumeroChanging(value);
this.SendPropertyChanging();
this._numero = value;
this.SendPropertyChanged("numero");
this.OnnumeroChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_idpersona", DbType="Int NOT NULL")]
public int idpersona
{
get
{
return this._idpersona;
}
set
{
if ((this._idpersona != value))
{
if (this._persona.HasLoadedOrAssignedValue)
{
throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
}
this.OnidpersonaChanging(value);
this.SendPropertyChanging();
this._idpersona = value;
this.SendPropertyChanged("idpersona");
this.OnidpersonaChanged();
}
}
}
[global::System.Data.Linq.Mapping.AssociationAttribute(Name="persona_telefono", Storage="_persona", ThisKey="idpersona", OtherKey="id", IsForeignKey=true)]
public persona persona
{
get
{
return this._persona.Entity;
}
set
{
persona previousValue = this._persona.Entity;
if (((previousValue != value)
|| (this._persona.HasLoadedOrAssignedValue == false)))
{
this.SendPropertyChanging();
if ((previousValue != null))
{
this._persona.Entity = null;
previousValue.telefonos.Remove(this);
}
this._persona.Entity = value;
if ((value != null))
{
value.telefonos.Add(this);
this._idpersona = value.id;
}
else
{
this._idpersona = default(int);
}
this.SendPropertyChanged("persona");
}
}
}
public event PropertyChangingEventHandler PropertyChanging;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void SendPropertyChanging()
{
if ((this.PropertyChanging != null))
{
this.PropertyChanging(this, emptyChangingEventArgs);
}
}
protected virtual void SendPropertyChanged(String propertyName)
{
if ((this.PropertyChanged != null))
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
#pragma warning restore 1591