What is the best way to implement update row if it exists, else insert new row logic using Entity Framework?
Below is what I have done so far. I want to check, if any field in the existing employee database has changed then only update that record or if it is a new one add as a new row.
Ex- Update the job title if it has changed, or add it as a new line if a new employee is added
//DbContext
public class DataContext : DbContext
{
public static string providerName = "System.Data.SqlClient";
public DbSet<DisplayAPIDataEmployee>? Employee { get; set; }
protected override void OnConfiguring(Microsoft.EntityFrameworkCore.DbContextOptionsBuilder optionBuilder)
{
optionBuilder.UseSqlServer("Server=;Initial Catalog = ;user id = ;password=");
}
protected override void OnModelCreating(Microsoft.EntityFrameworkCore.ModelBuilder modelBuilder)
{
modelBuilder.Entity<DisplayAPIDataEmployee>().ToTable("Employee", e => e.IsTemporal());
}
}
// Data model
[Table("Employee")]
public class DisplayAPIDataEmployee
{
public DisplayAPIDataEmployee()
{
createdOn = DateTime.Now;
}
public DateTime ?createdOn { get; set; }
public string ?displayName { get; set; }
public string ?shortBirthDate { get; set; }
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public string employee_id { get; set; }
}