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=)]
public int StudentKey1 { get; set; } [Key]
[Column(Order=)]
public int StudentKey2 { get; set; } [MaxLength()]
[ConcurrencyCheck]
[Required]
[Column("SName",Order=,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=)]
public int StudentKey1 { get; set; } [Key]
[Column(Order=)]
public int StudentKey2 { get; set; } [MaxLength()]
[ConcurrencyCheck]
[Required]
[Column("SName",Order=,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无效解决办法
EF(EF Core)中的NotMappedAttribute(转载)的更多相关文章
- 第五节:EF Core中的三类事务(SaveChanges、DbContextTransaction、TransactionScope)
一. 说明 EF版本的事务介绍详见: 第七节: EF的三种事务的应用场景和各自注意的问题(SaveChanges.DBContextTransaction.TransactionScope). 本节主 ...
- 在ASP.NET Core中通过EF Core实现一个简单的全局过滤查询
前言 不知道大家是否和我有同样的问题: 一般在数据库的设计阶段,会制定一些默认的规则,其中有一条硬性规定就是一定不要对任何表中的数据执行delete硬删除操作,因为每条数据对我们来说都是有用的,并且是 ...
- [小技巧]EF Core中如何获取上下文中操作过的实体
原文地址:https://www.cnblogs.com/lwqlun/p/10576443.html 作者:Lamond Lu 源代码:https://github.com/lamondlu/EFC ...
- ASP.NET Core中使用GraphQL - 第六章 使用EF Core作为持久化仓储
ASP.NET Core中使用GraphQL ASP.NET Core中使用GraphQL - 第一章 Hello World ASP.NET Core中使用GraphQL - 第二章 中间件 ASP ...
- EF Core中避免贫血模型的三种行之有效的方法(翻译)
Paul Hiles: 3 ways to avoid an anemic domain model in EF Core 1.引言 在使用ORM中(比如Entity Framework)贫血领域模型 ...
- EF Core中的多对多映射如何实现?
EF 6.X中的多对多映射是直接使用HasMany-HasMany来做的.但是到了EF Core中,不再直接支持这种方式了,可以是可以使用,但是不推荐,具体使用可以参考<你必须掌握的Entity ...
- EF Core中执行Sql语句查询操作之FromSql,ExecuteSqlCommand,SqlQuery
一.目前EF Core的版本为V2.1 相比较EF Core v1.0 目前已经增加了不少功能. EF Core除了常用的增删改模型操作,Sql语句在不少项目中是不能避免的. 在EF Core中上下文 ...
- EF Core中DbContext可以被Dispose多次
我们知道,在EF Core中DbContext用完后要记得调用Dispose方法释放资源.但是其实DbContext可以多次调用Dispose方法,虽然只有第一次Dispose会起作用,但是DbCon ...
- 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)
原文地址:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx EF 6 Code-F ...
随机推荐
- BZOJ5322: [JXOI2018]排序问题
传送门 不难看出期望就是 \(\frac{(n+m)!}{\prod_{v=1}^{max}(cnt_v!)}\),\(cnt_v\) 表示 \(v\) 这个数出现的次数. 贪心就是直接把 \(m\) ...
- HTML(5)基础
1.html常用标签 <pre>...</pre>:标识预定义文本 <a>是anchor的缩写,<a>标签定义锚点和超链接,<a>常与hre ...
- 盲刷bios
本帖最后由 evayh 于 2011-12-17 13:09 编辑 先看看是否是insyde的bios,如果是的话,可以救回来 insyde BIOS在损坏时,会自动进入CRISIS MODE试图刷回 ...
- html 手机web超出屏幕宽度的内容不换行,并产生横向滚动条
html 手机web超出屏幕宽度的内容不换行,并产生横向滚动条 white-space: nowrap;overflow-x: scroll;
- MBTIles实现
MBTIles实现 3.1 Compliant(符合) python: raster2mb (write) python: mbutil (read/write) python: landez (wr ...
- Linux —— Vi 命令介绍
简介 vi编辑器是所有Unix及Linux系统下标准的编辑器,它的强大不逊色于任何最新的文本编辑器. 这里只是简单地介绍一下它的用法和一小部分指令. 由于对Unix及Linux系统的任何版本,vi编辑 ...
- post请求的四种数据格式
1.application/json:这是最常见的 json 格式:{"input1":"xxx","input2":"ooo&q ...
- OpenResty 安装配置
0. 说明 1. Windows 下安装 下载软件包 openresty-1.13.6.1-win32.zip ,解压即可食用. [开启] 直接运行 nginx.exe 在 Windows 的命令窗口 ...
- [IDEA_6] IDEA 集成 Python
0. 说明 在 IDEA 中集成 Python 1. IDEA 集成 Python 1.1 Ctrl + Alt + S 进入设置 依次选中 Settings --> Plugins -- ...
- mysql 大文件导入导出
导出:mysqldump -u用户名 -p密码 -hIP地址 数据库名 > /dump.sql示例:mysqldump -uroot -proot -h127.0.0.1 test > / ...