实体框架中实现通用持久方法的最佳解决方案是什么?
例如有一个公司实体:
public class Company
{
public Company()
{
Id = Guid.NewGuid();
}
public Guid Id { get; set; }
public string Name { get; set; }
}
Id 属性可以在实体存储到数据库之前生成。
所以任务是实现 Persist 方法:
public class Repository
{
private DbSet<Company> _dbSet;
public void Persist(Company company)
{
// How to implement body here to Add entity if it doesn't exists yet
// or Modify it in opposit case?
// In terms of database entity record is required to be inserted or updated.
}
}
谢谢!