locked
New ID RRS feed

  • Question

  •  

    I have the following function in my repo class, if I create an Customer object pass it in, how would I get the newly generated identity ID?

    public async Task<int> AddAsync<T>(T t) where T : class
            {
                _context.Set<T>().Add(t);
                return await _context.SaveChangesAsync();
            }

    Monday, November 2, 2015 4:28 PM

Answers

  • EF populates the Identity primary-key back to the ID property of the object when the record is inserted into the database. Therefore, you can address the object after the insert of the object into the database to get the value of the primary-key ID the inserted object has assigned to it.
    Monday, November 2, 2015 6:40 PM