简体   繁体   English

数据库更改时如何在数据库优先方法ASP.NET“核心”中更新模型(添加了新表)

[英]How to update the model in Database First Approach ASP.NET “Core” when the DB changes (A new table is added)

I am using ASP.NET core 2.0 MVC template and have a couple of issues: 我正在使用ASP.NET Core 2.0 MVC模板,并且有几个问题:

i)I generated the models by scaffolding using database first approach given in MSDN Documentation . 我使用MSDN文档中给出的数据库优先方法通过脚手架生成模型。 But now I have added a new table (Employees) and wish to include that table as a model. 但是现在我添加了一个新表(Employees),并希望将该表作为模型。 But when I run this command : 但是当我运行此命令时:

Scaffold-DbContext "Server=server;Database=db;Trusted_Connection=True;" 
Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Table Employees - Force

then it overwrites my previous dbcontext class(removes all previous models and adds only Employee in it).How can I add only model related to that new table only keeping the previous models as such. 然后它将覆盖我以前的dbcontext类(删除所有以前的模型,并仅在其中添加Employee)。如何仅添加与该新表相关的模型,而又仅保留以前的模型。

ii)similarly I can't figure out the way how to update a existing model if the table corresponding to it is modified(added new columns using sql server).If I scaffold then it erases all the code written in that model class earlier. ii)同样,如果修改了对应的表(使用sql server添加了新列),我也想不出如何更新现有模型的方法。

I can't find out any proper documentation for database first approach apart from the MSDN one which doesn't deal with updating the models when underlying DB changes. 除了MSDN(在基础数据库更改时不处理模型更新)之外,我找不到用于数据库优先方法的任何适当文档。

Thanks 谢谢

You must delete the first one model and create a new, re-scaldfold all that you need 您必须删除第一个模型并创建一个新的,按比例缩放的所有所需内容

delete Model.Context.tt, Model.tt, Model.edmx 删除Model.Context.tt,Model.tt,Model.edmx

delete Entities string from Web.config 从Web.config删除实体字符串

Create the EDMX and Context files the same way you did for the first tim 创建EDMX和Context文件的方式与您对第一时间的处理方式相同

as @Valuator that is for Code first method 作为@Valuator,用于代码优先方法

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

相关问题 如何使用数据库优先方法读取 asp.net 内核中的表 - How to read tables in asp.net core, with database first approach 如何在ASP.NET MVC Core中使用数据库优先方法在现有数据库中实现Asp.net身份 - How to implement Asp.net identity with existing database using database first approach in asp.net mvc core 如何在 ASP.NET Core 中更新现有的脚手架数据库的表 - How to update a table of an existing, scaffolded database in ASP.NET Core 如何使用数据库优先方法更新 .Net Core 中的数据库上下文类? - How to update DB context class in .Net Core using database first approach? 如何在ASP.NET MVC和数据库优先方法中具有不同的类名和不同的表名 - How to have different class names and different table names in asp.net mvc + Database First Approach 如何将Asp.net身份与数据库一起使用 - how to Use Asp.net Identity with DataBase first approach 将模型添加到单独的表时,ASP.NET MVC AJAX更新选择列表项 - ASP.NET MVC AJAX Update select list items when model is added to separate table ASP.NET Core 2.0 Identity添加新模型-表到数据库 - ASP.NET Core 2.0 Identity add new model - table to the Database 数据库更改时更新ASP.NET数据绑定的GridView - Update ASP.NET databound GridView when database changes Asp.net Core 2.0与MYSQL的实体框架数据库优先方法 - Asp.net core 2.0 with entity framework database first approach with MYSQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM