在MVC 4中未显示部分实体类的数据注释 [英] Data Annotations not showing for partial Entity classes in MVC 4

查看:195
本文介绍了在MVC 4中未显示部分实体类的数据注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了几十条关于如何通过部分类将元数据注释添加到通过实体框架生成的类的说明,数据库首先。有人可以告诉我为什么这些新的显示值在我的观点中没有显示?这两个都是与我的实体框架生成类相同的命名空间的一部分。

  [MetadataType(typeof(xRef_CodesMetadata))] 
public partial class xRef_Codes
{
}

public class xRef_CodesMetadata
{
public int CodeID {get;组; }
public int CTB_ID {get;组; }

[必需(ErrorMessage =请键入名称)]
[显示(名称=代码名称)]
[列(TypeName =代码名称 )]
public string CodeName {get;组; }

[必需(ErrorMessage =请输入描述)]
[Display(Name =Description)]
[Column(TypeName =Description
public string描述{get;组; }
}

分段视图:

 < th> 
@ Html.DisplayNameFor(model => model.OfCodeID)
< / th>
< th>
@ Html.DisplayNameFor(model => model.CodeName)
< / th>
< th>
@ Html.DisplayNameFor(model => model.Description)
< / th>


解决方案

这已经解决了!我已经看了30个甚至40个教程,为什么这个实体框架数据库优先的部分类不工作。然后我发现这个帖子给出了以下建议:


抱歉这是这么晚,但我刚刚解决了一个类似的情况。 I
相信该行



[MetadataType(typeof(CompanyMD))]



属于由EF生成的部分类,即使当您更改模型时,将会擦除
。所以你的EF生成的文件
应该看起来像这样:


看到剩下的文章去这个链接.. 。
MVC 4 EF5数据库首在部分类中设置默认值


I have seen dozens of explanations of how to add metadata annotations, via partial classes, to classes generated via Entity Framework, database first.

Can someone tell me why these new display values are not showing in my views? Both of these are part of the same namespace as my entity framework generated classes.

[MetadataType(typeof(xRef_CodesMetadata))]
        public partial class xRef_Codes
        {
        }

public class xRef_CodesMetadata
    {
        public int CodeID { get; set; }
        public int CTB_ID { get; set; }

        [Required(ErrorMessage = "Please type a name")]
        [Display(Name = "Code Name")]
        [Column(TypeName = "Code Name")]
        public string CodeName { get; set; }

        [Required(ErrorMessage = "Please type a Description")]
        [Display(Name = "Description")]
        [Column(TypeName = "Description")]
        public string Description { get; set; }     
    }

Fragment of View:

<th>
    @Html.DisplayNameFor(model => model.OfCodeID)
</th>
<th>
    @Html.DisplayNameFor(model => model.CodeName)
</th>
<th>
    @Html.DisplayNameFor(model => model.Description)
</th>   

解决方案

This has been resolved! I have looked at literally 30 maybe 40 tutorials about why this Entity Framework "Database First" partial classes wasn't working. Then I found this post that gave the following suggestion:

Sorry this is so late, but I just solved a similar scenario myself. I believe the line

[MetadataType(typeof(CompanyMD))]

belongs in the partial class generated by the EF, even though it will be erased if and when you change the model. So your EF-generated file should look like this:

To see the rest of the post go this link... MVC 4 EF5 Database First set Default Values in Partial Class

这篇关于在MVC 4中未显示部分实体类的数据注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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