EF(EF Core)中的NotMappedAttribute(转载)

本文深入解析了Entity Framework Code-First中NotMapped特性的使用方法,包括如何阻止属性映射到数据库,解决NotMappedAttribute无效的问题,以及在DbContext的OnModelCreating方法中如何忽略特定属性。

NotMapped特性可以应用到EF实体类的属性中,Code-First默认的约定,是为所有带有get,和set属性选择器的属性创建数据列。。
NotManpped特性打破了这个约定,你可以使用NotMapped特性到某个属性上面,然后Code-First就不会为这个属性在数据表中创建列了。
我们看一下下面的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EF2
{
    [Table("StudentMaster",Schema="WaHaHa")]
   public class Student
    {
        [Key]
        [Column(Order=5)]
        public int StudentKey1 { get; set; }

        [Key]
        [Column(Order=6)]
        public int StudentKey2 { get; set; }

        
        [MaxLength(20)]
        [ConcurrencyCheck]
        [Required]
        [Column("SName",Order=1,TypeName="nvarchar")]
        public string StudentName { get; set; }

        [NotMapped()]
        public int? Age { get; set; }


        public int StandardRefId { get; set; }

        [ForeignKey("StandardRefId")]
        public virtual Standard Standard { get; set; }

      

    }
}

 

注意到没有,这个表里面没有Age列。


但是如果属性,只有Get属性访问器,或者只有set属性访问器,那么Ef Code-First就不会为它创建数据列了。
请看:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EF2
{
    [Table("StudentMaster",Schema="WaHaHa")]
   public class Student
    {
        [Key]
        [Column(Order=5)]
        public int StudentKey1 { get; set; }

        [Key]
        [Column(Order=6)]
        public int StudentKey2 { get; set; }

        
        [MaxLength(20)]
        [ConcurrencyCheck]
        [Required]
        [Column("SName",Order=1,TypeName="nvarchar")]
        public string StudentName { get; set; }

        [NotMapped()]
        public int? Age { get; set; }


        public int StandardRefId { get; set; }


        public string FirstName 
        {
            get { return FirstName; }
        }

        public int myAge;
        public int MyAge 
        {
            set { value = myAge; }
        }

        [ForeignKey("StandardRefId")]
        public virtual Standard Standard { get; set; }



      

    }
}

得到的数据库还是这个:

 

 

NotMappedAttribute无效解决办法

可以通过NotMappedAttribute标记模型某个属性可以使该属性不必映射到数据库。

public class Unicorn
{
public int Id { get; set; }
[NotMapped]
public string Name { get; set; }

[Timestamp]
public byte[] Version { get; set; }

public int PrincessId { get; set; } // FK for Princess reference
public virtual Princess Princess { get; set; }
}

NotMapped无效的时候,在DbContext的OnModelCreating方法重载中实现

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    //不映射到数据库中
    modelBuilder.Entity<BlogArticle>().Ignore(p => p.Title); 
}

EF Core中Ignore方法的用法如下:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<BlogArticle>(entity =>
    {
        //不映射到数据库中
        entity.Ignore(p => p.Title);
    });
}

 

 

 

转载文章:

数据注解特性--NotMapped

EF 解除属性映射到数据库中 NotMappedAttribute无效解决办法

 

转载于:https://www.cnblogs.com/OpenCoder/p/10177062.html

一、基础信息数据集名称:工地安全装备检测数据集图片数量:- 训练集:493张图片- 验证集:60张图片- 测试集:64张图片分类类别:- helmet(安全帽):建筑工人头部防护装备- vest(反光背心):高可见性人身防护装备- person(施工人员):工地作业人员主体标注格式:YOLO格式,包含边界框坐标和类别标签,适用于目标检测任务数据来源:建筑工地监控视频帧及施工现场实拍图像二、适用场景1. 智能工地安全监控系统:自动检测施工人员安全装备佩戴情况(安全帽/反光背心),实时预警违规行为1. 安全生产管理平台:集成至建筑企业安全管理体系,自动生成装备合规率统计报表1. 自动化巡检机器人:为工地巡检机器人提供核心识别能力,替代人工安全检查1. 岗前安全培训系统:基于真实场景开发交互式培训工具,强化施工人员安全意识三、数据集优势场景真实性:- 全部数据源自真实建筑工地场景,覆盖不同光照条件、人员姿态及作业环境- 包含复杂背景干扰要素(机械/建材/遮挡物),提升模型鲁棒性标注专业性:- 严格遵循PPE(个人防护装备)检测标准标注- 三重质检流程确保边界框定位精准,类别标注零误差任务适配性:- 兼容YOLO系列、Faster R-CNN等主流检测框架- 支持安全装备合规检测、人员行为分析等多任务扩展行业针对性:- 专注建筑行业高频风险场景(高空作业/机械操作区域)- 覆盖安全监管核心需求,满足ISO安全标准验证要求
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值