Hello, My Session Object looks like below -
C#
public class Session
{
public int Id { get; set; }
public int StudentId { get; set; }
public string? SessionDetails { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public int Valid { get; set; }
}I wish to create a POST that will create an instance of session with StartTime as systemdate (EF: Add). After session is over I would call the EF : Update call to store the EndTime.
Question: Is there any way to get the system generated Id in response when I Add the instance? That way I can keep the Id and then use it to Update.

Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 13, 2023, 11:48 PM EDT
Hello Dave,
Try below code :
[HttpPost] public async Task CreateSession(Session session) { session.StartTime = DateTime.Now; _context.Sessions.Add(session); await _context.SaveChangesAsync(); return Ok(session.Id); }the
SaveChangesAsyncmethod is called after adding the new session instance to the database. This method will update theIdproperty of the session instance with the system-generated Id.Thanks
Naimish Makwana
Sachin SinghRank #66 · 26.2K reputationPosted Mar 14, 2023, 4:08 AM EDT
follow this
At session start
[HttpPost] public async Task CreateSession(Session session) { session.StartTime = DateTime.Now; _context.Sessions.Add(session); await _context.SaveChangesAsync(); return View(session); // or return json //and use the Id of session object to post when session ends }on session end
[HttpPost] public async Task UpdateSession(Session session) { var currentSession= _context.Sessions.where(x=>x.Id==session.Id); session.EndTime= DateTime.Now; //for new EF version _context.Sessions.Update(currentSession); await _context.SaveChangesAsync(); //end //for old EF version _context.entry(currentSession).State= EntityState.Modified; //end return View(Session); // or return json }Anandu G NathRank #681 · 3.5K reputationPosted Dec 28, 2023, 10:50 PM EST
Check Below Code for yor refrences
public class SessionController : ControllerBase { private readonly YourDbContext _context; public SessionController(YourDbContext context) { _context = context; } // POST: api/sessions/start [HttpPost("start")] public async Task> StartSession(Session session) { if (!ModelState.IsValid) { return BadRequest(ModelState); } // Set the start time to the current system date session.StartTime = DateTime.Now; _context.Sessions.Add(session); await _context.SaveChangesAsync(); return CreatedAtAction(nameof(GetSession), new { id = session.Id }, session); } // PUT: api/sessions/end/5 [HttpPut("end/{id}")] public async Task EndSession(int id) { var session = await _context.Sessions.FindAsync(id); if (session == null) { return NotFound(); } // Set the end time to the current system date when the session is over session.EndTime = DateTime.Now; _context.Entry(session).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SessionExists(id)) { return NotFound(); } else { throw; } } return NoContent(); } private bool SessionExists(int id) { return _context.Sessions.Any(e => e.Id == id); } }