EF(EF Core)中的NotMappedAttribute(转载)
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); }); }
转载文章:
EF 解除属性映射到数据库中 NotMappedAttribute无效解决办法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架