我有一个带有EF 2内核的ASP.NET core 2.1应用程序 我正在使用内置的DI 我像这样注册我的课程: 在Startup.cs中,在ConfigureServices中
var connectionString = this.configuration.GetConnectionString("MYConnection");
services.AddDbContext<IMyContext, MyContext>(
options => options.UseSqlServer(connectionString));
services.AddScoped<IMy1Dao, My1Dao>();
services.AddScoped<IMy2Dao, My2Dao>();
services.AddScoped<IMyService, MyService>();
我注入这样的类:
public class MyController : Controller, IMyController{
private readonly IMyService myService;
public MyController(IMyService myService)
{
this.myService = myService;
}
public async Task<List<MyBatchUIDTO>> GetMyBatches([FromBody]List<short> ids)
{
return await myService.GetMyBatches(ids);
}
}
public class MyService : IMyService{
private readonly IMy1Dao my1Dao;
private readonly IMy2Dao my2Dao;
public MyService(IMy1Dao my1Dao, IMy2Dao my2Dao)
{
this.my1Dao = my1Dao;
this.my2Dao = my2Dao;
}
}
public partial class MyContext : DbContext, IMyContext
{
public MyContext() { }
public MyContext(DbContextOptions<MyContext> options)
{
}
//left out DBSET<> details
}
public class My1Dao : IMy1Dao
{
private IMyContext context
public My1Dao(IMyContext context)
{
this.context = context
}
}
public class My2Dao : IMy2Dao
{
private IMyContext context
public My2Dao(IMyContext context)
{
this.context = context
}
}
但是我在MyService构造函数中得到了2个不同的MyContext实例。 我知道这一点是因为我在MyContext Constructora上遇到了断点,并且在那儿断了两次。 另外,当我从2个DAO中读取时,它们不共享返回的集合。 我需要他们做什么。