【发布时间】:2018-01-02 20:59:39
【问题描述】:
我正在尝试实现通用存储库模式,但我的“添加”方法有一个小问题:
public class GenericRepository<T> where T : class
{
private readonly DbContext _context;
private DbSet<T> DbSet;
public async Task AddAsync(T t)
{
_context.Set<T>().Add(t);
await _context.SaveChangesAsync();
}
}
此方法有效,但由于我的实体的“Id”属性是 Guid,我将全零作为 Id。
所以在我的方法中,我应该将新的 Guid 设置为 Id:
t.Id = Guid.NewGuid();
但我不确定如何通过泛型设置我的实体的 Id 属性。
【问题讨论】:
标签: c# generics repository-pattern