简体   繁体   中英

SQLite scaffolding with Entity Framework Core

When I run

Scaffold-DbContext "Filename=mydatabase.sqlite3" Microsoft.EntityFrameworkCore.Sqlite

I get an empty context

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

namespace MyNamespace
{
    public partial class mydatabaseContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite(@"Filename=mydatabse.sqlite3");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
        }
    }
}

Am I doing something wrong. Is this available for SQLite?

I have a single table in the database with id and name, just a simple example to get me going.

It is creating a new database in bin folder because of the relative path in the connection string. I used new connection string.

Scaffold-DbContext "DataSource=C:\dev\mydatabase.sqlite3" Microsoft.EntityFrameworkCore.Sqlite

Paths are resolved relative to the output directory. (aka bin .) Ensure your database file is copied to the output directory on build.

Do this in csproj by setting Copy to Output Directory to Copy if newer .

Do this on xproj by adding the following to your project.json .

{
  "publishOptions": {
    "include": [
      "mydatabase.sqlite"
    ]
  }
}

你可以试试:

Scaffold-DbContext "DataSource=PATH\mydatabase.sqlite3 "Microsoft.EntityFrameworkCore.Sqlite -OutputDir Models

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM