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

c# entity-framework-core sql-server

Question

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!

1
0
5/11/2019 5:56:12 AM

Popular Answer

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
0
5/11/2019 5:56:12 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow