簡體   English   中英

將新表添加到數據庫后如何更新 Entity Framework Core

[英]How to update Entity Framework Core after adding a new table to the database

https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db?toc=/aspnet/core/toc.json&bc=/aspnet/core/breadcrumb/toc.json&view= aspnetcore-2.2

我已經按照上面的說明進行操作並且它有效。 我有數據庫和 C# 代碼。

下一階段是我添加一個名為 test 的新表

  • 表:測試
  • 列:Testid (int)、testname (nvarchar(50))

這個測試表被添加到數據庫中。

代碼

Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

使您能夠從頭開始創建類及其數據成員,但這次我添加了一個新表。

代碼

Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

當我添加了一個新表時,它對維護不起作用。

主要問題:

我應該使用 PowerShell 中的哪些代碼或類似的代碼來更新與 Entity Framework Core 相關的當前 C# 代碼?

我需要在 VS 解決方案中為表“Test”提供 C# 代碼。

謝謝!

首先,您需要為表創建類,如下所示:

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

其次,在您的上下文模型中添加類名稱,如下所示

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

第三,您必須在控制台或程序包管理器中進行遷移,

add-migration makePostTabale

然后更新數據庫:

Update-Database

試試這個命令:

Scaffold-DbContext "Server=(localdb)\\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir 模型 -Force

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM