如何在数据库优先方法ASP.NET“核心"中更新模型.数据库更改时(添加了新表) [英] How to update the model in Database First Approach ASP.NET "Core" when the DB changes (A new table is added)

查看:372
本文介绍了如何在数据库优先方法ASP.NET“核心"中更新模型.数据库更改时(添加了新表)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET Core 2.0 MVC模板,并且有几个问题:

I am using ASP.NET core 2.0 MVC template and have a couple of issues:

i)我使用以下数据库中给出的数据库优先方法通过脚手架生成了模型 MSDN文档.但是现在我添加了一个新表(Employees),并希望将该表作为模型.但是当我运行此命令时:

i)I generated the models by scaffolding using database first approach given in MSDN Documentation. But now I have added a new table (Employees) and wish to include that table as a model. But when I run this command :

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

然后它会覆盖我以前的dbcontext类(删除所有以前的模型,并仅在其中添加Employee).如何仅添加与该新表相关的模型,而又仅保留以前的模型.

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.

ii)同样,如果修改了与之对应的表(使用sql server添加了新列),我也想不出如何更新现有模型的方法.早点上课.

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.

除了MSDN(在基础数据库更改时不处理更新模型的MSDN)之外,我找不到适合数据库优先方法的任何适当文档.

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.

谢谢

推荐答案

您必须删除第一个模型并创建一个新的,按比例缩放的所有所需内容

You must delete the first one model and create a new, re-scaldfold all that you need

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

delete Model.Context.tt, Model.tt, Model.edmx

从Web.config删除实体字符串

delete Entities string from Web.config

以与第一个时间相同的方式创建EDMX和上下文文件

Create the EDMX and Context files the same way you did for the first tim

作为@Valuator,用于代码优先方法

as @Valuator that is for Code first method

这篇关于如何在数据库优先方法ASP.NET“核心"中更新模型.数据库更改时(添加了新表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆