My controller needs two types of service and both need to have the same interface of dbcontext,
public class MyController : Controller
{
protected readonly ISomeService _some;
protected reaonly IUnitOfWork _unitOfWork
public MyController(IUnitOfWork unitOfWork, ISomeService some)
{
_some = some;
_unitOfWork = unitOfWork
}
//This is my configuration
services.AddDbContext<MyContext>(x =>
{
x.UseSqlServer(Configuration.GetConnectionString("coon"));
});
Entity Framework contexts
Entity Framework contexts should be added to the service container using the scoped lifetime. This is handled automatically with a call to the AddDbContext method when registering the database context. Services that use the database context should also use the scoped lifetime.
So AddDbContext would add it as a scoped life time.
You need to make sure that the controllers / or other places where this is used, are also added in DI as the Scoped items.
Scoped lifetime services are created once per request.
我发现Issuse我的问题是我使用AsyncSaveChanges insted SaveChanges,但我的服务得到了相同的instace谢谢大家:)!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.