我创建了新的 .net core 2.1 项目。我创建了我的类,如下所示。但是,我在MyRepos.cs
.
“MyDbContext”必须是具有公共无参数构造函数的非抽象类型,以便在泛型类型或方法“UnitOfWork”中将其用作参数“TContext”
UnitOfWork.cs
public class UnitOfWork<TContext> : IUnitOfWork<TContext> where TContext : DbContext, new()
{ }
IUnitOfWork.cs
public interface IUnitOfWork<U> where U : DbContext
{ }
MyRepos.cs
public class MyRepos : UnitOfWork<MyDbContext>, IMyRepos
{ }
IMyRepos.cs
public interface IMyRepos : IUnitOfWork<MyDbContext>
{ }
MyDbContext.cs
public class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions options) : base(GetOptions())
{ }
public static DbContextOptions GetOptions()
{
return SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder(), "myConnectionString").Options;
}
}