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. 配置Chrome Workspace功能

    配置Chrome Workspace功能 Chrome Workspace功能是将在Chrome开发者工具(F12)中对文档的修改直接应用于对应文件中.由于Chrome并不知道当然文档对应用的文件为哪 ...

  2. vba的一个File操作类

    Option Explicit '-------------------------------------------------------- '[Class name]: clsTxtFile ...

  3. 【C】——如何生成静态库和动态库

    对于一个编译型语言,编译器工作时总要经过预处理.编译.链接等几个过程.以 C/C++ 为例:预处理器(cpp)将每个 .c/.cpp 文件中 #include 的头文件都合并到 .c/.cpp 文件中 ...

  4. R语言判断向量中是否存在一个元素

    判断ori_data[,1]中是否存在元素a: a %in% ori_data[,1] 如果存在返回 true,否则返回 false    

  5. T-SQL 错误状态

    MS Windows Error Messages Code Error Message 0 操作成功完成. 1 功能错误. 2 系统找不到指定的文件. 3 系统找不到指定的路径. 4 系统无法打开文 ...

  6. Android学习之ListView使用基础

    1.ListView的简单用法 1.1 布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/andro ...

  7. 深入浅出LVM on linux

    什么是LVM? 什么是LVM?LVM(Logical Volume Manager)逻辑卷管理,是一种将一个或多个硬盘的分区在逻辑上集合,相当于一个大硬盘来使用,当硬盘的空间不够使用的时候,可以继续将 ...

  8. 自然语言交流系统 phxnet团队 创新实训 项目博客 (八)

    在本项目中使用到的“文本转语音”的技术总结: 文本转语音,使用的是科大讯飞的接口,因为此作品之中语音包不是重点,所以语音包的转换我们统一调用的科大讯飞的语音包接口,依旧是在线的文字转语音,客户端将来自 ...

  9. java不确定参数个数方法例子

    package test; public class myTest { // 根据输入的参数动态生成数组 public static int max(int... param) { int max = ...

  10. e803. 获得和设置JProgressBar的值

    // To create a progress bar, see e801 创建一个JProgressBar组件 // Get the current value int value = progre ...