0

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());


                    }
1
  • If it works, then yes it's the correct way. Jul 26, 2011 at 7:46

1 Answer 1

0

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:

var newPerson = new Person() { Name = "Windcape" };
people.Add(newPerson);

var id = newPerson.Id; // Id is now the inserted id
1
  • Claus: Wow. It is short and simple but I am not sure as have been used to the old way. please see my code above.
    – MilkBottle
    Jul 26, 2011 at 7:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.