title: SQLite Android Disk I/O Error 10 (266) when creating Database
body: <p>I'm setting up a new Xamarin Forms (Android) Application from Scratch and want to implement a SQLite Database with Entity Framework Core. No matter where I let EF Core create the database I get a SQLite Disk I/O Error 10 (Extended Error Code 266).</p>
  
  <p>Note: (I already created Xamarin Forms Applications with EF Core (SQLite) where the database could be created as I try it this time and no error like this occurred)</p>
  
  <ol>
  <li><p>I tried moving all EF related code away from the UI-Thread so I can be sure that the UI-Thread isn't locking the path of the applications folder or something like that.</p></li>
  <li><p>I tried different paths to create my database file.</p></li>
  <li><p>Async / Non-Async database creation by ef core.</p></li>
  <li><p>(I'm using DI with Autofac) I tried removing all Autofac related code and initializing the needed DatabseContext by myself.</p></li>
  <li><p>I made sure that I have all the needed permissions to write to a file/read from a file in Android.</p></li>
  </ol>
  
  <p>(6. I also tried to register my dependencies in different ways, to make sure Autofac or DI, in general, isn't the problem of the thrown error)</p>
  
  <p>In my android MainActivity I read out the database path and pass it to my Xamarin.Forms Application like this:</p>
  
  <pre><code>SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_e_sqlite3());
  string databasePath = Application.ApplicationContext.GetDatabasePath("target.db").AbsolutePath;
  </code></pre>
  
  <p>I also create an instance of AppSetup which is a class I use to let all needed dependencies being registered by autofac.</p>
  
  <pre><code>public AppSetup(string databasePath)
  {
      _databasePath = databasePath;
  }
  
  public IContainer CreateContainer()
  {
      var containerBuilder = new ContainerBuilder();
      RegisterDependencies(containerBuilder);
      return containerBuilder.Build();
  }
  
  protected virtual void RegisterDependencies(ContainerBuilder containerBuilder)
  {
      containerBuilder.RegisterType&lt;TargetContext&gt;().As&lt;ITargetContext&gt;().WithParameter("databasePath", _databasePath).SingleInstance();
      containerBuilder.RegisterType&lt;UnitOfWork&gt;().As&lt;IUnitOfWork&gt;().SingleInstance();
      containerBuilder.RegisterType&lt;SettingsRepository&gt;().As&lt;ISettingsRepository&gt;().SingleInstance();
  }
  </code></pre>
  
  <p>After all of this is done, I create the database file like this in my App.xaml.cs:</p>
  
  <pre><code>// Register types and create container
  AppContainer.Container = setup.CreateContainer();
  
  // Begin lifetime scope of container
  using var scope = AppContainer.Container.BeginLifetimeScope();
  
  var context = scope.Resolve&lt;ITargetContext&gt;();
  context.Initialize();
  </code></pre>
  
  <p>In context.Initialize() I let EF core create the database (if it isn't created already) and this is where the Exception is thrown:</p>
  
  <pre><code>public bool Initialize()
  {
      bool result = Database.EnsureCreated();
      return result;
  }
  </code></pre>
  
  <p>Note: The configuration of the DatabaseContext:</p>
  
  <pre><code>protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  {
      optionsBuilder.UseSqlite($"Filename={_databasePath}");
  
      base.OnConfiguring(optionsBuilder);
  }
  </code></pre>
  
  <p>I expect that EF core can create the SQLite database file without any problem. I looked up for a solution for this error for several days but nothing could help me solving the error. </p>
  
  <p>PS: The stacktrace I'm getting with this error:</p>
  
  <pre><code>  at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC (System.Int32 rc, SQLitePCL.sqlite3 db) [0x0006d] in &lt;99138fe6fbac4a1b964cb2ae266abca2&gt;:0 
    at Microsoft.Data.Sqlite.SqliteConnection.Open () [0x00130] in &lt;99138fe6fbac4a1b964cb2ae266abca2&gt;:0 
    at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection (System.Boolean errorsExpected) [0x00068] in &lt;f837de81a58c413490ebba0c6077cbc0&gt;:0 
    at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open (System.Boolean errorsExpected) [0x00042] in &lt;f837de81a58c413490ebba0c6077cbc0&gt;:0 
    at Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal.SqliteDatabaseCreator.Exists () [0x0000c] in &lt;971e586a59454fdbaaa76f95799fac73&gt;:0 
    at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureCreated () [0x00008] in &lt;f837de81a58c413490ebba0c6077cbc0&gt;:0 
    at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated () [0x0000b] in &lt;6f7983df5846436f8e9cb779867785c7&gt;:0 
    at Target.ServiceLayer.TargetContext.Initialize () [0x00002] in C:\Users\Nicklas\Source\Repos\XFTarget\Target\Target\ServiceLayer\TargetContext.cs:28 
  </code></pre>
  
  <p>EDIT (1): PS: I'm debugging my application on a real device, but other sample applications I created with xamarin forms + ef core can create their database successfully on this device.</p>