简体   繁体   English

带有 Entity Framework Core 的 SQLite 脚手架

[英]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?这可用于 SQLite 吗?

I have a single table in the database with id and name, just a simple example to get me going.我在数据库中有一个带有 id 和 name 的表,这只是一个让我开始的简单示例。

It is creating a new database in bin folder because of the relative path in the connection string.由于连接字符串中的相对路径,它正在 bin 文件夹中创建一个新数据库。 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. (又名bin 。)确保您的数据库文件在构建时复制到输出目录。

Do this in csproj by setting Copy to Output Directory to Copy if newer .通过将Copy to Output Directory设置为Copy if newer在 csproj 中执行此操作。

Do this on xproj by adding the following to your project.json .通过将以下内容添加到您的project.json在 xproj 上执行此操作。

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

你可以试试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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