Maciek
Maciek

Reputation: 181

SQLite.CodeFirst - The context cannot be used while the model is being created

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:

  1. 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; }
    
    }
    
  2. MainWindowViewModel - just to initialize database

    #region Constructor
    public MainWindowViewModel()
    {
        using (var context = new GestDbContext())
        {
            var entity = context.Gestures.Any();
        }
    }
    #endregion
    
  3. App.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>

Upvotes: 2

Views: 825

Answers (1)

mircea
mircea

Reputation: 131

I saved modelBuilder from OnModelCreating in a instance variable

private DbModelBuilder _modelBuilder;
override protected void OnModelCreating(DbModelBuilder modelBuilder)
{
    _modelBuilder = modelBuilder;
}

then used it in a public method:

public void Init()
{
  var model = _modelBuilder.Build(Database.Connection);
  IDatabaseCreator sqliteDatabaseCreator = new SqliteDatabaseCreator();
  sqliteDatabaseCreator.Create(Database, model);
}

and executed it once (i need to create the structure once)

public MainWindowViewModel()
{
  using (var context = new GestDbContext())
  {
    var entity = context.Gestures.Any();
    context.Init();
  }
}

Upvotes: 1

Related Questions