Hi Sudip_inn,
I use WPF and EF 6.0 to make a data entry demo for your reference. Hope this can help you.
When add or edit data, I use your code.
Here is the snippet of the code.
public void Save(object parameter)
{
if (parameter!=null && parameter is NewEmployeeWindow)
{
NewEmployeeWindow editWindow = parameter as NewEmployeeWindow;
try
{
AddOrModify<Employee>(editWindow.SelectedEmployee, editWindow.SelectedEmployee.MyKey);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
editWindow.Close();
}
Reload();
}
public void Reload()
{
using (var db = new MyContainer())
{
Employees = new ObservableCollection<Employee>(db.Employees.ToList());
}
}
public void AddOrModify<T>(T entity, string key) where T : class, IEntity // Implements MyKey
{
using (var context = new MyContainer())
{
if (context.Set<T>().Any(e => e.MyKey == key))
{
context.Entry(entity).State = EntityState.Modified;
}
else
{
context.Entry(entity).State = EntityState.Added;
}
context.SaveChanges();
}
}
Check my project Here:
https://1drv.ms/u/s!As1txuR5i35kj9MH4gbvSxUtg3DmWQ

Best Regards,
Bob
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to
MSDN Support, feel free to contact MSDNFSF@microsoft.com.