简体   繁体   English

Asp.NetCore 将表带入现有的 DbContext

[英]Asp.NetCore bringing table into existing DbContext

I have successfully created a DbContext with tables in asp.netcore.我已经成功地在DbContext创建了一个带有表的DbContext。

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.我知道.edmx文件和更新数据库模型在 .netcore 中不再可用,并且想知道如果dbcontext已经创建,是否可以自动将这些表生成到类中。

您需要使用 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.您需要使用 EntityFramework Core,只需在网上搜索它,您就可以获得大量有关如何操作的教程。 I learned it in only one day.我只用了一天就学会了。

You can add the tables in your DB and then Scaffold.您可以在数据库中添加表,然后添加 Scaffold。 Your models will be updated.您的模型将被更新。

To Scaffold, open Package Manager Console in Visual Studio and add the following line and enter:要 Scaffold,在 Visual Studio 中打开Package Manager Console并添加以下行并输入:

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;" Scaffold-DbContext "ServerYourServerName;Database=YourDBName;Trusted_Connection=True;UserId=YourUserId;Password=YourPassword;Integrated Security=false;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -f Microsoft.EntityFrameworkCore.SqlServer -OutputDir 模型 -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;" Scaffold-DbContext "ServerYourServerName;Database=YourDBName;Trusted_Connection=True;UserId=YourUserId;Password=YourPassword;Integrated Security=false;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -t "TableName"-f Microsoft.EntityFrameworkCore.SqlServer -OutputDir 模型 -t "TableName" -f

you need to download few Nuget Package Manager from visual Studio editor.您需要从 Visual Studio 编辑器下载一些Nuget 包管理器。

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

Just search above mentioned package in NuGet package manager and install with same versions which matches with asp.net core.只需在 NuGet 包管理器中搜索上述包,并使用与 asp.net 核心匹配的相同版本进行安装。

after this just update your model classes properly and add DbSet<> in Datacontext class.之后只需正确更新您的模型类并DbSet<> in Datacontext class.添加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.只需通过从 Visual Studio 代码中的上部菜单标题转到工具来打开包管理器控制台。 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它看起来像这样,您可以根据需要用任何东西替换 Identity

PM> add-migration identity

Build started... Build succeeded.构建开始... 构建成功。 To undo this action, use Remove-Migration.要撤消此操作,请使用 Remove-Migration。

PM> update-database

Build started... Build succeeded.构建开始... 构建成功。 Done.完毕。

just boom all your database will get update with new tables.只需繁荣您的所有数据库都将使用新表进行更新。

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

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