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);
});
}

转载文章:

数据注解特性--NotMapped

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

EF(EF Core)中的NotMappedAttribute(转载)的更多相关文章

  1. 第五节:EF Core中的三类事务(SaveChanges、DbContextTransaction、TransactionScope)

    一. 说明 EF版本的事务介绍详见: 第七节: EF的三种事务的应用场景和各自注意的问题(SaveChanges.DBContextTransaction.TransactionScope). 本节主 ...

  2. 在ASP.NET Core中通过EF Core实现一个简单的全局过滤查询

    前言 不知道大家是否和我有同样的问题: 一般在数据库的设计阶段,会制定一些默认的规则,其中有一条硬性规定就是一定不要对任何表中的数据执行delete硬删除操作,因为每条数据对我们来说都是有用的,并且是 ...

  3. [小技巧]EF Core中如何获取上下文中操作过的实体

    原文地址:https://www.cnblogs.com/lwqlun/p/10576443.html 作者:Lamond Lu 源代码:https://github.com/lamondlu/EFC ...

  4. ASP.NET Core中使用GraphQL - 第六章 使用EF Core作为持久化仓储

    ASP.NET Core中使用GraphQL ASP.NET Core中使用GraphQL - 第一章 Hello World ASP.NET Core中使用GraphQL - 第二章 中间件 ASP ...

  5. EF Core中避免贫血模型的三种行之有效的方法(翻译)

    Paul Hiles: 3 ways to avoid an anemic domain model in EF Core 1.引言 在使用ORM中(比如Entity Framework)贫血领域模型 ...

  6. EF Core中的多对多映射如何实现?

    EF 6.X中的多对多映射是直接使用HasMany-HasMany来做的.但是到了EF Core中,不再直接支持这种方式了,可以是可以使用,但是不推荐,具体使用可以参考<你必须掌握的Entity ...

  7. EF Core中执行Sql语句查询操作之FromSql,ExecuteSqlCommand,SqlQuery

    一.目前EF Core的版本为V2.1 相比较EF Core v1.0 目前已经增加了不少功能. EF Core除了常用的增删改模型操作,Sql语句在不少项目中是不能避免的. 在EF Core中上下文 ...

  8. EF Core中DbContext可以被Dispose多次

    我们知道,在EF Core中DbContext用完后要记得调用Dispose方法释放资源.但是其实DbContext可以多次调用Dispose方法,虽然只有第一次Dispose会起作用,但是DbCon ...

  9. 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)

    原文地址:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx EF 6 Code-F ...

随机推荐

  1. Geolocation API

    Geolocation API--地理定位 navigator.geolocation getCurrentPosition() 触发请求用户共享地理定位信息的对话框 接收3个参数: 1.成功回调函数 ...

  2. Map对象以及for of的使用方法

  3. [WPF 基础知识系列] —— 绑定中的数据校验Vaildation

    前言: 只要是有表单存在,那么就有可能有对数据的校验需求.如:判断是否为整数.判断电子邮件格式等等. WPF采用一种全新的方式 - Binding,来实现前台显示与后台数据进行交互,当然数据校验方式也 ...

  4. Python Django框架笔记(二):创建应用和django 管理

    #前提是已经创建项目 (一)      创建应用 使用命令,在项目中创建一个应用(blog自定义) python manage.py startapp blog 创建完成后,可以看到下面几个文件 文件 ...

  5. centos7 下安装 nginx-1.12.2

    官网地址:http://nginx.org/2017.html 转载:https://blog.csdn.net/harris135/article/details/78576263 1.环境: ce ...

  6. JSP 过滤器

    JSP教程 - JSP过滤器 JSP过滤器是可用于拦截来自客户端的请求或处理来自服务器的响应的Java类. 过滤器可用于执行验证,加密,日志记录,审核. 我们可以将过滤器映射到应用程序部署描述符文件w ...

  7. 迁移MSSQL实例的所有login(包含密码)

    迁移数据库的时候肯定会涉及到login的迁移(包含数据库除外). 而一般我们迁移login的时候,可能会使用在某个login上右键生成脚本这样的做法.但是这样生成的脚本不能把密码也生成出来. 而且你只 ...

  8. [Spark RDD_add_1] groupByKey & reduceBykey 的区别

    [groupByKey & reduceBykey 的区别] 在都能实现相同功能的情况下优先使用 reduceBykey Combine 是为了减少网络负载 1. groupByKey 是没有 ...

  9. You are not late! You are not early!

    Do you think you are going No Where in Life? STOP! Take a deep breathe THINK! New York is three hour ...

  10. vc使用jsoncpp头文件冲突问题

    编译时出现 1>d:\program files (x86)\microsoft visual studio 9.0\vc\include\xdebug(32) : warning C4229: ...