You need a "Base Model" from where you will inherit the unique ID.
public class BaseModel<t> where T : BaseModel<t>
{
[Key, Required, DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
}
public class PersonModel : BaseModel<personmodel>
{
public string Name { get; set; }
public DateTime Birth{ get; set; }
}
And of course add it to your entityframework context
public class SMSServiceContext : DbContext
{
public SMSServiceContext()
: base()
{
}
public DbSet<personmodel> PersonModels{ get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
The unique Key will be automatic and unique. This is how I do it when I work with entityframework anyways.
I hope I answered your question. If not, please inform me :)