欢迎光临
个人技术文档整理

EF Core 数据批注 - [NotMapped ] 属性

未映射属性:[NotMapped]

NotMapped 属性可以应用于我们不想在数据库中为其创建相应列的实体类的属性。 默认情况下,EF 为每个属性创建一个列(必须具有 get; 和set;)在实体类中。 该属性将覆盖此默认约定。可以将属性应用于不希望在数据库表中为其创建相应列的一个或多个属性。 

 

    using System.ComponentModel.DataAnnotations;  
    //[NotMapped]  // 此表不会在数据库中创建 
    public class Blog
    {

        public long Id { get; set; }

        [NotMapped]//此列不会在数据库中创建相关的列
        public string KeyId { get; set; }

        public string Title { get; set; } = string.Empty;

        public string? Content { get; set; }

        /// <summary>
        /// 只有get  也不会创建相关列
        /// </summary>
        public string? Content2 { get { return Content; } }


        private string _Content3;
        /// <summary>
        /// 只有set 也不会创建相关列
        /// </summary>
        public string Content3 { set { _Content3 = value; } }
    }

生成结果

赞(2)