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