9

Ho seguito la guida Getting Started on ASP.NET 5 su Entity Framework 7 e ho sostituito MicrosoftSqlServer con Sqlite, l'unica differenza nel codice è in Avvio. cs:ASP.NET 5, EF 7 e SQLite - Errore SQLite 1: 'nessuna tabella di questo tipo: Blog'

services.AddEntityFramework() 
    .AddSqlite() 
    .AddDbContext<BloggingContext>(options => options.UseSqlite("Filename=db.db")); 

quando eseguo il sito e passare a/blog, ottengo un errore:

Microsoft.Data.Sqlite.SqliteException was unhandled by user code
ErrorCode=-2147467259 HResult=-2147467259 Message=SQLite Error 1: 'no such table: Blog' Source=Microsoft.Data.Sqlite
SqliteErrorCode=1 StackTrace: at Microsoft.Data.Sqlite.Interop.MarshalEx.ThrowExceptionForRC(Int32 rc, Sqlite3Handle db) at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior) at Microsoft.Data.Sqlite.SqliteCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.ExecuteReader() at Microsoft.Data.Entity.Query.Internal.QueryingEnumerable.Enumerator.MoveNext() at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at System.Linq.Enumerable.d__1`2.MoveNext() at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at Microsoft.Data.Entity.Query.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at EFGetStarted.AspNet5.Controllers.BlogsController.Index() in d:\arthur\documents\visual studio 2015\Projects\EFGetStarted.AspNet5\src\EFGetStarted.AspNet5\Controllers\BlogsController.cs:regel 18 InnerException:

ho capito questo, come se non v'è alcuna tabella chiamata 'Blog', ma quando apro il file .db in DB Browser per SQLite, in realtà esiste una tabella chiamata "Blog":

Screenshot from DB Browser for SQLite showing a table called 'Blog'

fa SQLite richiede altri cambiamenti nel codice, o si tratta di un errore nel connettore SQLite per Entity Framework?

+0

cosa fa il tuo 'metodo BlogsController.Index' assomiglia? – DavidG

+0

ti connetti al database giusto? – DevilSuichiro

+0

@DavidG 'public IActionResult Index() {return View (_context.Blogs.ToList()); } ' –

risposta

3

È molto probabile che il database venga effettivamente aperto da EF non sia il file che si sta aprendo in DB Browser. SQLite utilizza la directory di lavoro corrente del processo, che se lanciata in IIS o in altri server può essere una cartella diversa dalla directory del codice sorgente. (Vedere numeri https://github.com/aspnet/Microsoft.Data.Sqlite/issues/132 e https://github.com/aspnet/Microsoft.Data.Sqlite/issues/55).

Per garantire che il file db sia nel posto giusto, utilizzare un percorso assoluto. Esempio:

public class Startup 
{ 
    private IApplicationEnvironment _appEnv; 

    public Startup(IApplicationEnvironment appEnv) 
    { 
     _appEnv = appEnv; 
    } 
    public void ConfigureServices(IServiceCollection services) 
    { 
     services.AddEntityFramework() 
      .AddSqlite() 
      .AddDbContext<MyContext>(
       options => { options.UseSqlite($"Data Source={_appEnv.ApplicationBasePath}/data.db"); }); 
    } 
} 
+0

Grazie mille per aver condiviso questa risposta. Mi sono imbattuto in questo utilizzando ASP.NET Core 1.0. Ho avuto la mia origine dati specificata in appsettings.json e non mi piaceva la variabile così ho fornito un percorso hard-coded come questo: "ConnectionStrings": { "DefaultConnection": "Data Source = C: \\ aspnetcoresqllitedbs \\ data \\ WebApplication.db " } –

0

Ho avuto questo problema su netcoreapp2.0. C'è un relativo issue che potrebbe essere in errore, ma non volevo risolverlo andando a una compilazione notturna.

La soluzione per me era creare e passare un SqliteConnection invece di utilizzare la stringa del builder.

Così, per questa configurazione:

 string id = string.Format("{0}.db", Guid.NewGuid().ToString()); 
     var builder = new SqliteConnectionStringBuilder() 
     { 
      DataSource = id, 
      Mode = SqliteOpenMode.Memory, 
      Cache = SqliteCacheMode.Shared 
     }; 

Compose per il DI in questo modo:

 var connection = new SqliteConnection(builder.ConnectionString); 
     connection.Open(); 
     connection.EnableExtensions(true); 
     services.AddDbContext<SomeDbContext>(options => options.UseSqlite(connection)); 

L'errore che ho avuto è stato con questo stile di init:

 services.AddDbContext<SomeDbContext>(options => options.UseSqlite(builder.ConnectionString)); 

mio ponteggi ha anche una chiamata una tantum a:

  var dbContext = serviceScope.ServiceProvider.GetService<SomeDbContext>(); 
      dbContext.Database.OpenConnection(); 
      dbContext.Database.EnsureCreated(); 

Utilizzando questo approccio tutte le mie copie istanzate da DI di SomeDbContext punterebbero tutte a un db SQLite valido e che DB avrebbe uno schema creato automaticamente come per le mie entità.

0

Sembra che le cose siano cambiate perché IApplicationEnvironment è stato sostituito con IHostingEnvironment.

Removing IApplicationEnvironment \ IRuntimeEnvironment

public class Startup 
{ 
    private IHostingEnvironment _appHost; 

    public Startup(IHostingEnvironment appHost) 
    { 
     _appHost = appHost; 
    } 
    public void ConfigureServices(IServiceCollection services) 
    { 
     services.AddEntityFrameworkSqlite() 
      .AddDbContext<MyContext>(
       options => { options.UseSqlite($"Data Source={_appHost.ContentRootPath}/data.db"); }); 
    } 
} 
Problemi correlati