Ado.net is not in Windows phone, so how do I get the primary key after an Insert operation?
There is the method in SQL:
SELECT @@IDENTITY
So, for SQl Ce in Windows phone, How to do it with Linq To SQL or whatever.
Thanks
—Updated :
Is this the onInsert you refer in the column ?
[Column(Storage="_CtcId", AutoSync=AutoSync.OnInsert, DbType="Int NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int CtcId
{
get
{
return this._CtcId;
}
set
{
if ((this._CtcId != value))
{
this.OnCtcIdChanging(value);
this.SendPropertyChanging();
this._CtcId = value;
this.SendPropertyChanged("CtcId");
this.OnCtcIdChanged();
}
}
}
I got the return ID. Please advise if this is the correct way to get primary key after insert operation such as this:
using (DBContacts context = new DBContacts(ConnectionString))
{
//--- create object first:
TblContacts tblCtc = new TblContacts();
tblCtc.FirstName = txtFirstName.Text.Trim();
tblCtc.LastName =txtLastName.Text.Trim();
tblCtc.Birthday = txtBirthday.Text.Trim();
tblCtc.NickName = txtNickName.Text.Trim();
context.TblContacts.InsertOnSubmit(tblCtc);
context.SubmitChanges();
var id = tblCtc.CtcId;
MessageBox.Show("Inserted Ok. Id is no :" + id.ToString());
}
If your objects PrimaryKey property have AutoSync set to OnInsert , then once you inserted a object, the object will automatically have it’s key updated.
Example: