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

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

问题描述

我已经看到了几十如何添加元数据注释的解释,通过部分类,通过实体框架,数据库第一次生成的类

谁能告诉我,为什么这些新的显示值是不是在我的观点展示?这两者都是同一个命名空间为我的实体框架生成的类的一部分。

  [MetadataType(typeof运算(xRef_ codesMetadata))]
        公共部分类xRef_ codeS
        {
        }公共类xRef_ codesMetadata
    {
        公众诠释$ C $ {CID获取;组; }
        公众诠释CTB_ID {搞定;组; }        [必需(的ErrorMessage =请输入名称)]
        [显示(NAME =code名称)]
        [专栏(类型名=code名称)]
        公共字符串$ C $ {CNAME获得;组; }        [必需(的ErrorMessage =请输入一个描述)
        [显示(名称=说明)]
        [专栏(类型名=说明)]
        公共字符串描述{搞定;组; }
    }

查看的片段:

 <第i个
    @ Html.DisplayNameFor(型号=> model.Of codeID)
< /第i
<第i个
    @ Html.DisplayNameFor(型号=方式>模式codeNAME)
< /第i
<第i个
    @ Html.DisplayNameFor(型号=> model.Description)
< /第i


解决方案

这已经解决了!我看过关于为什么这个实体框架数据库优先部分类是行不通的字面30也许40教程。然后我发现这个职位给了以下建议:


  

很抱歉,这是这么晚了,但我只是解决了类似的情况我自己。一世
  相信行


  
  

[MetadataType(typeof运算(CompanyMD))]


  
  

属于由EF产生的部分类,即使它会
  当您更改模型,如果被擦除和。所以,你的EF-生成的文件
  应该是这样的:


要看到帖子的休息去吧此链接...
<一href=\"http://stackoverflow.com/questions/13616791/mvc-4-ef5-database-first-set-default-values-in-partial-class\">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天全站免登陆