Program.cs:
Код: Выделить всё
builder.Services.AddDbContext(opt => opt.UseInMemoryDatabase("My"));
// Can't work out how to wire up the Repository?
//builder.Services.AddScoped(p => new TDBContext());
//builder.Services.AddScoped();
builder.Services.AddScoped(typeof(IRepository), typeof(Repository));
//builder.Services.AddScoped(typeof(IRepository), typeof(Repository));
builder.Services.AddScoped();
var app = builder.Build(); //ERROR HERE!
Код: Выделить всё
public class MyService : IMyService
{
private readonly IRepository _repository;
public MyService(IRepository repository)
{
_repository = repository;
}
}
public class Repository : IRepository where TDBContext : DbContext
{
protected DbContext dbContext;
public Repository(DbContext context)
{
dbContext = context;
}
public async Task CreateAsync(T entity) where T : class
{
this.dbContext.Set().Add(entity);
return await this.dbContext.SaveChangesAsync();
}
//.....
}
public class TDBContext : DbContext
{
public TDBContext(DbContextOptions options)
: base(options)
{
}
public virtual DbSet Transactions { get; set; } = null!;
public TDBContext()
{
}
}
Подробнее здесь: https://stackoverflow.com/questions/716 ... ervice-typ