Data Annotations not showing for partial Entity classes in MVC 4

这一生的挚爱 提交于 2019-12-21 20:47:10

问题


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>   

回答1:


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




回答2:


This may or may not help someone else but after following this tutorial (https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/database-first-development/enhancing-data-validation).

I also had the similar problem of my metadata not reflecting in my view. The solution for me was having my metadata classes in the same namespace as my .edmx file, this line was the key "..., and each matches the name and namespace as the class that is automatically generated."




回答3:


You have to declare xRef_CodesMetadata class also as partial is as below.

public partial 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; }     
        }

May be useful for you Generating EF Code First model classes from an existing database and Adding Annotations for Data and Model-First Entities

I hope this will help to you.



来源:https://stackoverflow.com/questions/14412468/data-annotations-not-showing-for-partial-entity-classes-in-mvc-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!