EDMx 模型 - 数据注释

Edmx 模型 internel

public partial class ItemRequest
{
    public int RequestId { get; set; }
    //...
}

向此添加数据注释 - 如果我们直接修改此模型,则在对模型进行更新时,更改将丢失。所以

在这种情况下添加属性必需

创建一个新类 - 任何名称然后

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

//make sure the namespace is equal to the other partial class ItemRequest
namespace MvcApplication1.Models 
{
    [MetadataType(typeof(ItemRequestMetaData))]
    public partial class ItemRequest
    {
    }

    public class ItemRequestMetaData
    {
        [Required]
        public int RequestId {get;set;}

        //...
    }
}

要么

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace YourApplication.Models
{
    public interface IEntityMetadata
    {
        [Required]
        Int32 Id { get; set; }
    }

    [MetadataType(typeof(IEntityMetadata))]
    public partial class Entity : IEntityMetadata
    {
        /* Id property has already existed in the mapped class */
    }
}