简体   繁体   中英

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

I have followed this instruction above and it works. I have the database and the C# code.

The next phase is that I add a new table named test

  • Table: test
  • Columns: Testid (int), testname (nvarchar(50))

This test table is added in the database.

The code

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

enables you to create the class and its data member from scratch but this time I have added a new table.

The code

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

does not work in relation to maintenance when I have added a new table.

The main question:

What code in PowerShell or something similar should I use in order to update the current C# code in relation to Entity Framework Core?

I need to have c# code for the table "Test" in VS solution.

Thank you!

at first you need to make class for a table, like this:

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

second, add class name in your context model, like this

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

third, in console or package manager you must make a migration,

add-migration makePostTabale

and after that update database:

Update-Database

Try this cmd:

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

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