mvc 4 - c# - 实体框架 - 使用edmx文件的元数据

时间:2015-09-10 02:08:10

标签: entity-framework asp.net-mvc-4

我正在使用Entity Framework edmx。当我更新edmx时,我丢失了元数据。你如何创建和使用元数据属性(不是真正的字段)。

1 个答案:

答案 0 :(得分:2)

在项目中创建一个Metadata文件夹,其中是edmx。使用您的实体名称创建一个类如下:并在同一文件中创建另一个类,其实体名称为“metadata”扩展名(MyEntityMetadata)。

namespace MyNameSpace.DataAccess //You need to use the same namespace of edmx entities files
{
    [MetadataType(typeof(MyEntityMetaData))]
    public partial class MyEntity //This is possible because entities files using partial class
    {
        [NotMapped] //System.ComponentModel.DataAnnotations.Schema
        public int MyProperty { get; set; }


       //more properties...
     }

    public class UsuarioMetaData
    {
        [Display(ResourceType = typeof(Resources.Global), Name = "MyFieldLabel")]
        public int MyField { get; set; }

       //More fields
     }

}