0
我正在用实体框架在dotnet核心中创建一个webapi应用程序。当我通过文档。我可以使用DI在dotnet核心中注入dbcontext对象。但是当我使用一个dbcontext对象完成整个应用程序时。如何使dbcontext成为瞬态?如果有任何例子,它会真的帮助我。dotnet核心中的Dbcontext对象瞬态
请在下面找到我现有的代码。
这是我在ConfigureService
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DataAccess.XXXXContext>(options => options.UseMySQL(Configuration["ConnectionStrings:DefaultConnection"]),ServiceLifetime.Transient);
}
写的代码这是我的DbContext类
public partial class XXXXContext : DbContext
{
private readonly Logger logger = LogManager.GetCurrentClassLogger();
public XXXXContext(DbContextOptions<XXXXContext> options) :base(options)
{
logger.Debug("XXXXContext created");
}
}
写道如果你的代码请参阅我在AddDbContext方法中已经写入了Transient。因此,每次都创建对象。我的构造函数应该调用。但是我只打了一次电话。
我们需要看你的代码伙计...... – victor
我编辑了我的文章的源代码 – Vipin