尽管 ScaffoldColumn(true) 仍不显示键列
我正在使用 EF CodeFirst 开发 ASP.NET MVC 3 项目。我有一个简单的类,在关键列上设置了很少的属性:
public class Tag
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[ScaffoldColumn(true)]
public short TagID { get; set; }
[Required]
[MaxLength(50)]
public string Name { get; set; }
}
如您所见,有 DatabaseGenerate(DatabaseGenerateOption.None) 和 ScaffoldColumn(true) 属性。那是因为我希望能够手动输入 TagID。现在,当 TagControler 添加到项目中时,我在 5 个生成的视图中都没有显示 TagID 列。
我知道我可以手动添加它,但我想知道这种行为是设计使然还是我做错了什么?
I'm working on ASP.NET MVC 3 project using EF CodeFirst. I have a simple class with few attributes set on key column:
public class Tag
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[ScaffoldColumn(true)]
public short TagID { get; set; }
[Required]
[MaxLength(50)]
public string Name { get; set; }
}
As you can see there are DatabaseGenerated(DatabaseGeneratedOption.None) and ScaffoldColumn(true) attributes. That's because I want to be able to enter the TagID manually. Now when TagControler is added to the project I don't have the TagID column shown in neither of 5 generated views.
I know I can add it manually, but I wonder if this behavior is by design or I'm doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,主键不作为可编辑字段构建。相反,有一个隐藏的密钥字段。如果您想更改此行为,您可以修改模板,但这是设计使然,因为通常编辑实体的主键是没有意义的。
如果您想在添加视图时进行此更改或想对脚手架进行其他一些自定义更改,以下是有关如何执行此操作的一些信息:
http://blogs.msdn.com/b/joecar/archive/2011/01/06/add-the-asp-net-mvc-3-code-templates-to-your-application- with-nuget.aspx
Primary keys aren't scaffolded as editable fields by default. Instead there is a hidden field for the key. If you wanted to change this behavior you could modify the templates but it is by design since generally it doesn't make sense to edit the primary key of an entity.
Here is some info on how to do this if you wanted to make this change any time you added a view or wanted to make some other custom change to the scaffolding:
http://blogs.msdn.com/b/joecar/archive/2011/01/06/add-the-asp-net-mvc-3-code-templates-to-your-application-with-nuget.aspx