为什么在构架视图时应指定数据上下文类? [英] Why should I specify a data context class when scaffolding a view?

查看:93
本文介绍了为什么在构架视图时应指定数据上下文类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下图所示的添加视图"对话框向ASP.NET MVC 5项目添加新视图时,我被邀请选择一个模板和一个模型类,这使我可以快速生成一个表单来创建新的实例.模型或显示模型属性的视图.但是,为什么视图应该关心数据上下文类是什么?在我的项目中,无论我是否指定数据上下文类,都会生成相同的视图,但是我猜测可能会有所不同.可能是什么?

When adding a new view to an ASP.NET MVC 5 project using the Add View dialog pictured below, I'm invited to choose a template and a model class, which allows me to quickly generate a form for creating new instances of the model or a view that displays the model's properties. But why should the view care what the data context class is? In my project, whether or not I specify the data context class, the same view is generated, but I'm guessing there's a scenario where it would make a difference. What might that be?

推荐答案

如果您引用现有的DbContext,则向导将在您的DbContext派生类中插入public DbSet<Employee> Employee { get; set; }(如果尚不存在).看起来像Visual Studio在做一些辅助工作.

If you refer to an existing DbContext then the wizard will insert public DbSet<Employee> Employee { get; set; } (if it doesn't already exist) in your DbContext derived class . Looks like Visual Studio doing some of the leg work.

这篇关于为什么在构架视图时应指定数据上下文类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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