I'm working with WPF aplication with SQLite db. Trying to design data base with code first approach with SQLite.CodeFirst.
The Error is: {"The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe."}
Im using the code:
DbContext:
class GestDbContext : DbContext { override protected void OnModelCreating(DbModelBuilder modelBuilder) { var model = modelBuilder.Build(Database.Connection); IDatabaseCreator sqliteDatabaseCreator = new SqliteDatabaseCreator(); sqliteDatabaseCreator.Create(Database, model); } public DbSet<GestUser> GestUsers { get; set; } public DbSet<Gesture> Gestures { get; set; } public DbSet<UserSettings> UserSettings { get; set; } }MainWindowViewModel - just to initialize database
#region Constructor public MainWindowViewModel() { using (var context = new GestDbContext()) { var entity = context.Gestures.Any(); } } #endregionApp.config
<entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="mssqllocaldb" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" /> <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" /> </providers> </entityFramework> <system.data> <DbProviderFactories> <remove invariant="System.Data.SQLite.EF6" /> <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" /> </DbProviderFactories> </system.data> <connectionStrings> <add name="connection" connectionString="Data Source=.\GestDB" providerName="System.Data.SqlLite"/> </connectionStrings>