简体   繁体   中英

Asp.NetCore bringing table into existing DbContext

I have successfully created a DbContext with tables in asp.netcore.

However, I have realized there are a few more tables that I need to add. I know that .edmx files and updating the database model are no longer available in .netcore and was wondering if it is possible to automatically generate these tables into classes if the dbcontext has already been created.

您需要使用 CLI/Package Console 学习 Entity Framework 核心迁移,它将创建迁移快照类文件,您需要检查应用迁移,您可以参考Migrations - EF Core with ASP.NET Core

You need to use EntityFramework Core, and just search it up online, you can get a ton of tutorials on how to do it. I learned it in only one day.

You can add the tables in your DB and then Scaffold. Your models will be updated.

To Scaffold, open Package Manager Console in Visual Studio and add the following line and enter:

Scaffold-DbContext "ServerYourServerName;Database=YourDBName;Trusted_Connection=True;UserId=YourUserId;Password=YourPassword;Integrated Security=false;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -f

I know I am late to answer, I hope it will help others.

There is two way to do that.

  1. Get all the table from database from this query write in package manager console.

    Scaffold-DbContext "ServerYourServerName;Database=YourDBName;Trusted_Connection=True;UserId=YourUserId;Password=YourPassword;Integrated Security=false;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -f

  2. if you want specific table from database then run this query.

    Scaffold-DbContext "ServerYourServerName;Database=YourDBName;Trusted_Connection=True;UserId=YourUserId;Password=YourPassword;Integrated Security=false;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -t "TableName"-f

you need to download few Nuget Package Manager from visual Studio editor.

  1. Microsoft.aspnetcore.EntityFrameworkcore
  2. Microsoft.EntityFrameworkCore.SqlServer
  3. Microsoft.EntityFrameworkCore.Tools

Just search above mentioned package in NuGet package manager and install with same versions which matches with asp.net core.

after this just update your model classes properly and add DbSet<> in Datacontext class.

after you have completed all your work . just open package-manager console by going tools from upper menu header in visual studio code. then write below mentioned command.

PM> add-migration

second after completing build write next command

PM> update-database.

It will look like this you can replace Identity with anything as you wish

PM> add-migration identity

Build started... Build succeeded. To undo this action, use Remove-Migration.

PM> update-database

Build started... Build succeeded. Done.

just boom all your database will get update with new tables.

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