With my searches for an answer I came upon this question, How to insert a new record which has a auto increment number as primary key? , which I was hoping would solve my issue. But unfortunately it does not as I have the primary key set in the database.
I can't update the database using LINQ DataContext
, and updating via SubmitChanges
throws no error, it just does not update. It is my understanding that I don't need to create an autogenerated number in C# as the database should create the unique ID for this record.
This is the code I am using to try to update the database on an Auto-Increment primary key column.
Table newContext = new Table();
newContext.Staff_No = row.Staff_No;
newContext.Year_No = row.Year_No;
newContext.Month_No = row.Month_No;
dataContext.Tables.InsertOnSubmit(newContext);
try
{
dataContext.SubmitChanges();
}
catch (Exception ex)
{
Alerts.Error(@"Did not save", @"Error", ex);
}
The above code does not work, but it does when the database column is set to primary key ,but NOT auto increment. I do have to assign the ID
column a unique ID however.
I just add this to the code above and on a non-auto-increment column the data updates,
newContext.ID = DateTime.UtcNow.Ticks;
Is there something I need to be doing to get this to update the changes correctly?
This is the settings in SQL SERVER
EDIT: The auto generated class,
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Staff_Calc_TBL")]
public partial class Staff_Calc_TBL : INotifyPropertyChanging, INotifyPropertyChanged
{
private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);
private long _ID;
private System.Nullable<int> _Staff_No;
private System.Nullable<int> _Year_No;
private System.Nullable<int> _Month_No;
private System.Nullable<int> _Column_Index;
private System.Nullable<decimal> _Column_Count_Value;
private string _Column_DataVal;
#region Extensibility Method Definitions
partial void OnLoaded();
partial void OnValidate(System.Data.Linq.ChangeAction action);
partial void OnCreated();
partial void OnIDChanging(long value);
partial void OnIDChanged();
partial void OnStaff_NoChanging(System.Nullable<int> value);
partial void OnStaff_NoChanged();
partial void OnYear_NoChanging(System.Nullable<int> value);
partial void OnYear_NoChanged();
partial void OnMonth_NoChanging(System.Nullable<int> value);
partial void OnMonth_NoChanged();
partial void OnColumn_IndexChanging(System.Nullable<int> value);
partial void OnColumn_IndexChanged();
partial void OnColumn_Count_ValueChanging(System.Nullable<decimal> value);
partial void OnColumn_Count_ValueChanged();
partial void OnColumn_DataValChanging(string value);
partial void OnColumn_DataValChanged();
#endregion
public Staff_Calc_TBL()
{
OnCreated();
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ID", AutoSync=AutoSync.OnInsert, DbType="BigInt NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public long 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="_Staff_No", DbType="Int")]
public System.Nullable<int> Staff_No
{
get
{
return this._Staff_No;
}
set
{
if ((this._Staff_No != value))
{
this.OnStaff_NoChanging(value);
this.SendPropertyChanging();
this._Staff_No = value;
this.SendPropertyChanged("Staff_No");
this.OnStaff_NoChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Year_No", DbType="Int")]
public System.Nullable<int> Year_No
{
get
{
return this._Year_No;
}
set
{
if ((this._Year_No != value))
{
this.OnYear_NoChanging(value);
this.SendPropertyChanging();
this._Year_No = value;
this.SendPropertyChanged("Year_No");
this.OnYear_NoChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Month_No", DbType="Int")]
public System.Nullable<int> Month_No
{
get
{
return this._Month_No;
}
set
{
if ((this._Month_No != value))
{
this.OnMonth_NoChanging(value);
this.SendPropertyChanging();
this._Month_No = value;
this.SendPropertyChanged("Month_No");
this.OnMonth_NoChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Column_Index", DbType="Int")]
public System.Nullable<int> Column_Index
{
get
{
return this._Column_Index;
}
set
{
if ((this._Column_Index != value))
{
this.OnColumn_IndexChanging(value);
this.SendPropertyChanging();
this._Column_Index = value;
this.SendPropertyChanged("Column_Index");
this.OnColumn_IndexChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Column_Count_Value", DbType="Decimal(4,2)")]
public System.Nullable<decimal> Column_Count_Value
{
get
{
return this._Column_Count_Value;
}
set
{
if ((this._Column_Count_Value != value))
{
this.OnColumn_Count_ValueChanging(value);
this.SendPropertyChanging();
this._Column_Count_Value = value;
this.SendPropertyChanged("Column_Count_Value");
this.OnColumn_Count_ValueChanged();
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Column_DataVal", DbType="NVarChar(50)")]
public string Column_DataVal
{
get
{
return this._Column_DataVal;
}
set
{
if ((this._Column_DataVal != value))
{
this.OnColumn_DataValChanging(value);
this.SendPropertyChanging();
this._Column_DataVal = value;
this.SendPropertyChanged("Column_DataVal");
this.OnColumn_DataValChanged();
}
}
}
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));
}
}
}