EntityFramework Core 学习笔记 —— 添加主键约束
原文地址:[https://docs.efproject.net/en/latest/modeling/keys.html][1]
Keys (primary)
Key 是每个实体例的主要唯一标识。EF Core中的 Key 映射到关系型数据库中就相当于主键。我们也可以配置一个不是主键的唯一标识给一个实体。(点这里 [Alternate Keys][2] 查看更多信息)
内容导航
- [约定][3]
- [Data Annotation][4]
- [Fluent API][5]
约定
依照约定,一个名称为 `Id` 或者 `Id` 的属性将会被配置为实体的 Key。
class Car
{
public string Id { get; set; } // 人工高亮
public string Make { get; set; }
public string Model { get; set; }
}
class Car
{
public string CarId { get; set; } // 人工高亮
public string Make { get; set; }
public string Model { get; set; }
}
Data Annotations
我们也可以使用 Data Annotations 来配置一个属性,使之成为主键。
```
class Car
{
[Key] // 人工高亮
public string LicensePlate { get; set; }
public string Make { get; set; }
public string Model { get; set; }
}
<h2 id="3">Fluent API</h2>
我们也可以使用 Fluent API 来配置一个属性,使之成为主键。
class MyContext : DbContext
{
public DbSet Cars { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Car>().HasKey(c => c.LicensePlate); // 人工高亮
}
}
class Car
{
public string LicensePlate { get; set; }
public string Make { get; set; }
public string Model { get; set; }
}
当然了,我们也可以使用 Fluent API 来配置多个属性复合组成实体的 Key (在关系型数据库中,我们称之为复合主键)。**复合主键只能通过 Fluent API 设置,约定以及 Data Annotations 都无法设置复合主键。**
class MyContext : DbContext
{
public DbSet Cars { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Car>()
.HasKey(c => new { c.State, c.LicensePlate }); // 人工高亮
}
}
class Car
{
public string State { get; set; }
public string LicensePlate { get; set; }
public string Make { get; set; }
public string Model { get; set; }
}
[1]: https://docs.efproject.net/en/latest/modeling/keys.html
[2]: #todo
[3]: #1
[4]: #2
[5]: #3
EntityFramework Core 学习笔记 —— 添加主键约束的更多相关文章
- SQL反模式学习笔记5 外键约束【不用钥匙的入口】
目标:简化数据库架构 一些开发人员不推荐使用引用完整性约束,可能不使用外键的原因有一下几点: 1.数据更新有可能和约束冲突: 2.当前的数据库设计如此灵活,以至于不支持引用完整性约束: 3.数据库为外 ...
- Sql Server约束的学习一(主键约束、外键约束、唯一约束)
一.约束的分类 1.实体约束 实体约束是关于行的,比如某一行出现的值不允许出现在其他行,例如主键约束. 2.域约束 域约束是关于列的,对于所有行,某一列有那些约束,例如检查约束. 3.参照完整性约束 ...
- EntityFramework Core 学习笔记 —— 创建模型
原文地址:https://docs.efproject.net/en/latest/modeling/index.html 前言: EntityFramework 使用一系列的约定来从我们的实体类细节 ...
- MongoDB学习笔记~ObjectId主键的设计
回到目录 说一些关于ObjectId的事 MongoDB确实是最像关系型数据库的NoSQL,这在它主键设计上可以体现的出来,它并没有采用自动增长主键,因为在分布式服务器之间做数据同步很麻烦,而是采用了 ...
- EntityFramework Core 学习笔记 —— 包含与排除属性
原文地址:https://docs.efproject.net/en/latest/modeling/included-properties.html 在模型中包含一个属性意味着 EF 拥有了这个属性 ...
- EntityFramework Core 学习笔记 —— 包含与排除类型
原文地址:https://docs.efproject.net/en/latest/modeling/included-types.html 在模型类中包含一种类型意味着 EF 拥有了这种类型的元数据 ...
- mysql常用命令添加外键主键约束存储过程索引
数据库连接 mysql -u root -p123456 查看表 show databases 创建数据库设置编码 create table books character set utf8; 创建用 ...
- SQL反模式学习笔记22 伪键洁癖,整理数据
目标:整理数据,使不连续的主键Id数据记录变的连续. 反模式:填充断档的数据空缺. 1.不按照顺序分配编号 在插入新行时,通过遍历表,找到的第一个未分配的主键编号分配给新行,来代替原来自动分配的伪主键 ...
- Oracle表添加主键、外键
1.创建表的同时创建主键约束 (1)无命名 create table student ( studentid int primary key not null, studentname varchar ...
随机推荐
- JS 数组去重复值
var arr1 = [90, 91, 92]; var arr2 = [80, 81]; var arr3 = [80, 71, 72, 73]; var arr = arr1.concat(50, ...
- LeetCode Add Two Numbers II
原题链接在这里:https://leetcode.com/problems/add-two-numbers-ii/ 题目: You are given two linked lists represe ...
- jQuery点击收缩展开滑动显示内容竖直手风琴代码
<div class="position"> <div class="positiontop"> <span class=&quo ...
- 原生node的header
首先引入http模块 获取http.ServerResponse对象的方式,1.http.createServer(function(req,res){}) 其中res是http.ServerResp ...
- jQuery工具函数(转)
原文地址:http://www.cnblogs.com/kissdodog/archive/2012/12/27/2835561.html 作者:逆心 ------------------------ ...
- VitualBox环境下,实现windows系统与虚拟机Linux文件互传
本次环境是Win7系统和ubuntu14(虚拟机) 1.首先需要安装VitualBox的增强功能,如图所示 2.安装完成后重启linux系统,然后在WIN7系统下创建共享文件夹(本文在D盘下创建名为V ...
- ubuntu配置NFS
ubuntu配置NFS: sudo apt-get install nfs-kernel-server 配置/etc/exports 例如:我们要将根目录下的 /opt/FriendlyARM/min ...
- Timer的故事----Jdk源码解读
咱们今天也来说说定时器Timer Timer是什么? Timer n. [电子] 定时器:计时器:计时员 从翻译来看,我们可以知道Timer的本意是,定时定点. 而JDK中Timer类也的确是这个本 ...
- android 图片压缩
引用:http://104zz.iteye.com/blog/1694762 第一:我们先看下质量压缩方法: private Bitmap compressImage(Bitmap image) { ...
- 解决jquery mobile的header和footer在点击屏幕的时候消失的办法
给header和footer添加 data-position="fixed" 和 data-tap-toggle="false"即可,代码如下: <div ...