NotMapped特性可以应用到领域类的属性中,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; } }
}

得到的数据库还是这个:

数据注解特性--NotMapped的更多相关文章

  1. 7.10 数据注解特性--NotMapped

    NotMapped特性可以应用到领域类的属性中,Code-First默认的约定,是为所有带有get,和set属性选择器的属性创建数据列.. NotManpped特性打破了这个约定,你可以使用NotMa ...

  2. 7.7 数据注解特性--Table

    大家可能注意到,有几个特性,我没有翻译,因为实在是太简单了,看一下就知道,之前也学过,现在只是系统学一下,所以就粗略的看一下就行了. 现在学习数据注解特性的--Table特性. Table 特性可以被 ...

  3. 9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/stringlength-dataannotations-attribute-in-co ...

  4. 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribut ...

  5. 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in ...

  6. 9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】

    原文链接:http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.a ...

  7. 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】

    原文链接:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-firs ...

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

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

  9. 9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】

    原文地址:http://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first ...

随机推荐

  1. CPP_封装_继承_多态

    类的三方法:封装,继承,多态.封装:使用一整套方法去创建一个新的类型,这叫类的封装.继承:从一个现有的类型基础上,稍作改动,得到一个新的类型的方法,叫类的继承.多态:当有几个不同的子类对象时,对象调用 ...

  2. vba的一个DB操作类

    Option Explicit '-------------------------------------------------------- '[Class Name]: DBHelper '[ ...

  3. 使用 Gearman 实现分布式处理

    因为近来在研究 Mogilefs 的分布文件系统,在读读这个的源码,另外,为公司新设计了一个下载的系统,所以更加要深入研究一下,因为这个好东西是 Perl 写的,真不容易,在读这个的时间发现了几个好东 ...

  4. Git安装遇到的问题fatal: Could not read from remote repository.的解决办法

    转自:https://blog.csdn.net/huahua78/article/details/52330792 查看远端地址 git remote –v 查看配置 git config --li ...

  5. Python之FLAGS用法

    from absl import app from absl import flags FLAGS = flags.FLAGS flags.DEFINE_string('gpu', None, 'co ...

  6. python递归评论tree形图代码

    首先我有必要记录下来这段代码,因为我遇到了这个问题, 然后没有解决 后来, 前段说我找到一段代码给我看看, 我并没有在意, 然后她实现了, 她实现了,她真的实现了, 我... 为了感谢她,我陪她玩了一 ...

  7. 传统餐饮O2O支付体系成难题

    传统餐饮O2O支付体系成难题 网的数据化参考,使得门店运营更具科学性. 作为“易淘食”这样的订餐网站,主要依靠与餐厅分成 来获得利润.“根据每个餐厅合作情况不同,每一笔订单我们可获得5%-15%的佣金 ...

  8. shell编程小结

    因为项目中要用到shell脚本,所以系统的看了一下.以前只是泛泛的了解. 变量:环境变量.预定义变量.位置变量.自定义变量. 环境变量这个好说,通过set或者env命令都能看到相应的列表,然后可以通过 ...

  9. Aizu_Insertion Sort

    原题链接:https://vjudge.net/problem/Aizu-ALDS1_1_A 题目描述 Write a program of the Insertion Sort algorithm ...

  10. e741. 将标签的焦点置于关联的文本框上面

    This example associates a label with a text field using setLabelFor(). A mnemonic is set on the labe ...