首页 技术 正文
技术 2022年11月16日
0 收藏 872 点赞 4,850 浏览 1606 个字

NotMapped特性可以应用到领域类的属性中,Code-First默认的约定,是为所有带有get,和set属性选择器的属性创建数据列。。

NotManpped特性打破了这个约定,你可以使用NotMapped特性到某个属性上面,然后Code-First就不会为这个属性就不会在数据表中创建列了。

我们看一下下面的代码:

数据注解特性–NotMapped

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

数据注解特性–NotMapped

数据注解特性–NotMapped

注意到没有,这个表里面没有Age列。

但是如果属性,只有Get属性访问器,或者只有set属性访问器,那么Ef Code-First就不会为它创建数据列了。

请看:

数据注解特性–NotMapped

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

得到的数据库还是这个:

数据注解特性–NotMapped

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902