[英]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.