如何从强类型视图隐藏Entity Framework实体属性? [英] How to hide Entity Framework entity properties from strongly typed views?

查看:66
本文介绍了如何从强类型视图隐藏Entity Framework实体属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET MVC 4.0应用程序中使用实体框架,我想知道如何防止或隐藏实体在强类型视图中生成的字段?现在,我不需要的视图上正在生成几个主键字段和时间戳字段.

I am using Entity Framework in my ASP.NET MVC 4.0 application and I want to know how to prevent or hide fields from my entity from being generated in my strongly typed view? Right now several primary key fields and timestamp fields are being generated on the view which I do not want.

我知道将属性设置为internal而不是public是可行的,但是我不确定这将对整个下游产生什么影响.我更喜欢在属性上使用数据注释,但是我尝试使用的注释可以防止Controller脚手架或将其设置为隐藏字段.我希望它们保持公开状态,但不要在强类型视图中生成它们.

I know setting the property to internal as opposed to public works but I am not sure of the total downstream effect this will have. I prefer to use data annotations on the properties but the ones I have tried prevent Controller scaffolding or make them as hidden fields. I prefer for them to remain public but just not be generated in the strongly typed view.

要生成强类型视图,请在Visual Studio中添加一个新的视图",然后在对话框中选择要为其建模的类.反过来,这将创建一个视图,其中包含由类的属性表示的所有控件.例如,创建一个LastName字段,如下所示:

To generate a strongly typed View, add a new 'View' in Visual Studio and select the class in the dialog to which the view is modeled after. This in turn will create a view with all of the controls that are represented by properties on the class. For example a LastName field is created as below:

@Html.EditorFor(model => model.FirstName)

推荐答案

问题的答案

属性

[ScaffoldColumn(false)]

[Display(AutoGenerateField=false)]

在不需要的属性之前,de designer无法为这些属性生成脚手架字段.

before the unwanted properties will prevent de designer to generate scaffolding fields for those properties.

这篇关于如何从强类型视图隐藏Entity Framework实体属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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