[英]The context cannot be used while the model is being created. EF-Core ASP.net Core2.2
[英]“Context cannot be used while the model is being created” using ASP.NET Core Console Application With EF 6
當我使用帶有EF6
ASP.NET Core Console Application執行ASP.NET Core Console應用程序時。 我經常遇到以下錯誤。
System.InvalidOperationException:創建模型時不能使用上下文。 如果在OnModelCreating方法內部使用上下文,或者多個線程同時訪問同一上下文實例,則可能引發此異常。 請注意,不能保證DbContext和相關類的實例成員是線程安全的。 [01/02/2018 10:59:16> 395c4e:INFO]在System.Data.Entity.Internal.LazyInternalContext.InitializeContext()[01/02/2018 10:59:16> 395c4e:INFO]在System.Data System.Data.Entity.Internal.Linq.InternalSet
1.Initialize() [01/02/2018 10:59:16 > 395c4e: INFO] at System.Data.Entity.Internal.Linq.InternalSet
TypeEntityType)[01/02/2018 10:59:16> 395c4e:INFO]在1.Initialize() [01/02/2018 10:59:16 > 395c4e: INFO] at System.Data.Entity.Internal.Linq.InternalSet
1.get_InternalContext()[01/02/2018 10:59:16> 395c4e:INFO]在System.Data.Entity .Infrastructure.DbQuery1.System.Linq.IQueryable.get_Provider() [01/02/2018 10:59:16 > 395c4e: INFO] at System.Linq.Queryable.Select[TSource,TResult](IQueryable
1源,表達式`1選擇器)
這是我的啟動類文件:
public class Startup
{
private readonly IConfigurationRoot configuration;
public Startup()
{
string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{environment}.json", optional: true);
configuration = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
string sbConnectionString = configuration.GetValue<string>("ServiceBus:ConnectionString");
string subscriptionName = configuration.GetValue<string>("ServiceBus:Subscription");
bool isChinaRegion = configuration.GetValue<bool>("IsChinaRegion");
services.AddSingleton<IServiceBusSettings>(sbSettings => new ServiceBusSettings(sbConnectionString, subscriptionName, isChinaRegion));
string dbConnectionString = configuration.GetValue<string>("Database:ConnectionString");
var compactDbContext = new CompactDbContext(dbConnectionString);
services.AddTransient<DbContext>(dbContext => compactDbContext);
services.AddTransient<IRepository<UserBasicInfo>, UserBasicInfoRepository>();
services.AddTransient<IRepository<UserAdditionalInfo>, UserAdditionalInfoRepository>();
services.AddTransient<IRepository<UserRoles>, UserRolesRepository>();
services.AddTransient<IUserProfileRepository, UserProfileRepository>();
services.AddSingleton<IServiceBusObjectProcessingStrategy<SerivceBusModel.ContractInfo>, ContractInfoProcessingStrategy>();
services.AddTransient<IRepository<Registration>, RegistrationRepository>();
services.AddTransient<ITempRegistrationRepository, TempRegistrationRepository>();
services.AddLogging();
}
public void Configure(IServiceProvider serviceProvider)
{
if (configuration.GetValue<bool>("EnableDebugLogging"))
{
var loggerFactory = serviceProvider.GetService<ILoggerFactory>();
var logLevel = configuration.GetValue<LogLevel>("DebugLoggingMinLevel");
loggerFactory.AddConsole(logLevel);
loggerFactory.AddDebug(logLevel);
}
}
}
問題是您使用了Transient,但每次都返回相同的DbContext實例(即Singleton)。 但是,如果有多個人同時運行一個查詢,或者在多個線程上運行多個查詢,那么您將崩潰。 相反,您只需要這樣做:
services.AddTransient<DbContext>(dbContext => new CompactDbContext(dbConnectionString));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.