SQLite scaffolding with Entity Framework Core

十年热恋 提交于 2020-03-21 19:26:15

问题


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.


回答1:


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



回答2:


Paths are resolved relative to the output directory. (a.k.a. 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"
    ]
  }
}



回答3:


you can try:

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


来源:https://stackoverflow.com/questions/37984456/sqlite-scaffolding-with-entity-framework-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!