locked
Can't get auto-generated key value after adding a new record to a table RRS feed

  • Question

  • Hi

    I'm trying to get the
    auto-generated key value after adding a new record to a table. The "Using ADO.NET Data Services" document says:

    "After an entity is created or modified in the data service, the service returns a fresh copy of the entity including any values that may have been updated as a result of triggers in the database, auto-generated keys, etc. The client library will automatically update the .NET object with these new values."


    I tried as follows:


        public int Add(Record rec)
        {
          ctx = new WebDataContext(baseUri);
          ctx.MergeOption = MergeOption.AppendOnly;
          ctx.AddObject("Records", rec);
          ctx.SaveChanges();
          return rec.id;
        }

    but rec.id remains set to zero. The record on the database itself is correctly inserted with an assigned key value.
    I expect that I've misunderstood the documentation but would appreciate any advice.
    Friday, January 25, 2008 2:45 PM

Answers