从 .net 核心库访问存储库
Accssess to repository from .net core library
我需要在 .net 核心库的存储库 class 中包装一些函数。但是我得到一个错误,我的构造函数没有正确的构造函数。我究竟做错了什么?
这是我的 DBcontext 代码
public class effMercContext : DbContext
{
public DbSet<Department> Departments { get; set; }
public DbSet<Employee> Employees { get; set; }
public DbSet<Project> Projects { get; set; }
public effMercContext(DbContextOptions<effMercContext> options)
: base(options)
{
}
}
public class EffMercDbContextFactory : IDbContextFactory<effMercContext>
{
public effMercContext Create(DbContextFactoryOptions options)
{
var builder = new DbContextOptionsBuilder<effMercContext>();
builder.UseSqlServer("Server=(localdb)\mssqllocaldb;Database=pinchdb;Trusted_Connection=True;MultipleActiveResultSets=true");
return new effMercContext(builder.Options);
}
}
到目前为止我的存储库class
public class EmployeeRepository
{
public Employee GetByID(int id)
{
using (effMercContext db = new effMercContext())
{
}
}
}
那是因为你在这里调用了不带参数的构造函数:
public Employee GetByID(int id) {
using (effMercContext db = new effMercContext())
{
}
}
但是在你的 DbContext
class 中你只有一个带有一个参数的构造函数:
public class effMercContext : DbContext {
public effMercContext(DbContextOptions<effMercContext> options)
: base(options) {
}
}
查看这篇文章了解如何使用DbContextOptions
:https://docs.efproject.net/en/latest/miscellaneous/configuring-dbcontext.html
您可以按如下方式修改您的 EmployeeRepository
...
public class EmployeeRepository
{
var optionsBuilder = new DbContextOptionsBuilder<effMercContext>();
// add necessary options
public Employee GetByID(int id)
{
using (effMercContext db = new effMercContext(optionsBuilder.Options))
{
}
}
}
想了解更多可以关注this
我需要在 .net 核心库的存储库 class 中包装一些函数。但是我得到一个错误,我的构造函数没有正确的构造函数。我究竟做错了什么? 这是我的 DBcontext 代码
public class effMercContext : DbContext
{
public DbSet<Department> Departments { get; set; }
public DbSet<Employee> Employees { get; set; }
public DbSet<Project> Projects { get; set; }
public effMercContext(DbContextOptions<effMercContext> options)
: base(options)
{
}
}
public class EffMercDbContextFactory : IDbContextFactory<effMercContext>
{
public effMercContext Create(DbContextFactoryOptions options)
{
var builder = new DbContextOptionsBuilder<effMercContext>();
builder.UseSqlServer("Server=(localdb)\mssqllocaldb;Database=pinchdb;Trusted_Connection=True;MultipleActiveResultSets=true");
return new effMercContext(builder.Options);
}
}
到目前为止我的存储库class
public class EmployeeRepository
{
public Employee GetByID(int id)
{
using (effMercContext db = new effMercContext())
{
}
}
}
那是因为你在这里调用了不带参数的构造函数:
public Employee GetByID(int id) {
using (effMercContext db = new effMercContext())
{
}
}
但是在你的 DbContext
class 中你只有一个带有一个参数的构造函数:
public class effMercContext : DbContext {
public effMercContext(DbContextOptions<effMercContext> options)
: base(options) {
}
}
查看这篇文章了解如何使用DbContextOptions
:https://docs.efproject.net/en/latest/miscellaneous/configuring-dbcontext.html
您可以按如下方式修改您的 EmployeeRepository
...
public class EmployeeRepository
{
var optionsBuilder = new DbContextOptionsBuilder<effMercContext>();
// add necessary options
public Employee GetByID(int id)
{
using (effMercContext db = new effMercContext(optionsBuilder.Options))
{
}
}
}
想了解更多可以关注this