C# Question

How to get the id for insertion in to a DB in WebApi using Entity Framework?

Just experimenting with Azure & WebApi.

Have this model:

public class ModelClass
public int Id {get;set;}
public int Model {get;set;}

There is this method, which creates a new resource.

public IHttpActionResult PostModelClass(ModelClass obj)
if (!ModelState.IsValid)
return BadRequest(ModelState);


return CreatedAtRoute("DefaultApi", new { id = ModelClass.Id }, obj);

But, this forces me to supply a parameter called
which has to be unique.

How can I delegate this responsibility to the DB to create a new

I can, for the moment, think of getting the last updated id from the database & then supply it to the obj.

But is there any cleaner or any inbuilt way?


In standard circumstances (by convention ID is the primary key), ID property gets populated automatically by EF when you save changes to the database.