从.net核心库中获取存储库

时间:2016-10-26 08:57:24

标签: c# .net-core

我需要在.net核心库中的存储库类中包装一些函数。但我得到一个错误,我的构造函数没有正确的构造函数。我究竟做错了什么? 这是我的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);
    }
}

到目前为止我的存储库类

public class EmployeeRepository
{
    public Employee GetByID(int id)
    {
        using (effMercContext db = new effMercContext())
        {

        }
    }
}

2 个答案:

答案 0 :(得分:1)

那是因为你在这里调用没有参数的构造函数:

<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">

<span class="comment-likes">12</span>

但是在你的public Employee GetByID(int id) { using (effMercContext db = new effMercContext()) { } } 类中,你只有一个带有一个参数的构造函数:

DbContext

查看这篇文章,了解如何使用public class effMercContext : DbContext { public effMercContext(DbContextOptions<effMercContext> options) : base(options) { } } https://docs.efproject.net/en/latest/miscellaneous/configuring-dbcontext.html

答案 1 :(得分:0)

您可以将<TextView id="textFieldBody" hint="Some hint" text="{{ }}" editable="true" class="TextField" returnKeyType="done" /> 修改为以下内容......

EmployeeRepository

要了解详情,您可以关注this