【发布时间】:2021-05-09 19:20:13
【问题描述】:
我有 StudentDbContext
public class StudentDbContext : DbContext
{
public StudentDbContext()
{
}
public StudentDbContext(DbContextOptions<StudentDbContext> options)
: base(options)
{
}
public virtual DbSet<Students> Students{ get; set; }
}
然后我有一个存储库,我尝试了解注入 StudentDbContext 与注入 DbContextOptions 有什么区别
注入 DbContextOptions
class StudentRepository : IStudentRepository
{
private readonly DbContextOptions<StudentDbContext> _context;
public StudentRepository(DbContextOptions<StudentDbContext> context)
{
_context = context;
}
}
注入 StudentDbContext
class StudentRepository : IStudentRepository
{
private readonly StudentDbContext _context;
public StudentRepository(StudentDbContext context)
{
_context = context;
}
}
每种情况有什么优点或缺点吗?
【问题讨论】:
标签: asp.net-core .net-core entity-framework-core