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 */
    }
}