繁体   English   中英

使用 ASP.NET Core Web API 应用向 SQLite 添加新表

[英]Add new table to SQLite using an ASP.NET Core Web API app

我正在学习如何编写具有 Sqlite 数据库访问权限的 ASP.NET Core Web API 应用程序。

我在使用 Sqlite 数据库向我的应用程序添加新表时遇到问题。

我看到了 Microsoft 文章 how to create and initialize Sqlite database Getting Started with EF Core ),如下所示。

我试图添加

public DbSet<myClass> Test { get; set; }

刚过

public DbSet<Post> Posts { get; set; }

认为代码会自动添加Test表,但它不会(我正在使用 DB Browser for SQLite 工具查看数据库)。

我搜索了几十篇文章,但没有找到答案。 有人可以指出我正确的方向吗?

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;

public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Post { get; set; }

    public string DbPath { get; }

    public BloggingContext()
    {
        var folder = Environment.SpecialFolder.LocalApplicationData;
        var path = Environment.GetFolderPath(folder);
        DbPath = System.IO.Path.Join(path, "blogging.db");
    }

    // The following configures EF to create a Sqlite database file in the
    // special "local" folder for your platform.
    protected override void OnConfiguring(DbContextOptionsBuilder options)
        => options.UseSqlite($"Data Source={DbPath}");
}

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    public List<Post> Posts { get; } = new();
}

public class Post
{
    public int PostId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public int BlogId { get; set; }
    public Blog Blog { get; set; }
}

我尝试如下:首先,我生成 dbcontext

public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }

    }

然后在 Package Manage 控制台中输入 add- add- migration然后我使用您应该使用的博客和帖子模型更新了 dbcontext 并再次输入add- migration 现在,Migration Floder 中有两个类,如下所示:

迁移1:

public partial class _1 : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Blog",
                columns: table => new
                {
                    BlogId = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Url = table.Column<string>(type: "nvarchar(max)", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Blog", x => x.BlogId);
                });
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "Blog");
        }
    } 

迁移 2:

public partial class _2 : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.CreateTable(
            name: "Post",
            columns: table => new
            {
                PostId = table.Column<int>(type: "int", nullable: false)
                    .Annotation("SqlServer:Identity", "1, 1"),
                Title = table.Column<string>(type: "nvarchar(max)", nullable: true),
                Content = table.Column<string>(type: "nvarchar(max)", nullable: true),
                BlogId = table.Column<int>(type: "int", nullable: false)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Post", x => x.PostId);
                table.ForeignKey(
                    name: "FK_Post_Blog_BlogId",
                    column: x => x.BlogId,
                    principalTable: "Blog",
                    principalColumn: "BlogId",
                    onDelete: ReferentialAction.Cascade);
            });

        migrationBuilder.CreateIndex(
            name: "IX_Post_BlogId",
            table: "Post",
            column: "BlogId");
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropTable(
            name: "Post");
    }
}

然后,您可以通过在 Package Manage 控制台中输入update-database "migrationname"将数据库更新到您想要的版本。如果您已经将数据库更新到最新版本并希望恢复到以前的版本,

您也可以尝试update-database "former migrationname" ,如下所示: 在此处输入图像描述

暂无
暂无

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

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