栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用流畅的API设置唯一约束?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用流畅的API设置唯一约束?

EF6.2上 ,您可以

HasIndex()
用来添加索引以通过fluent API进行迁移。

https://github.com/aspnet/Entityframework6/issues/274

modelBuilder    .Entity<User>()    .HasIndex(u => u.Email)        .IsUnique();

EF6.1 开始,您可以使用

IndexAnnotation()
fluent API添加用于迁移的索引。

http://msdn.microsoft.com/zh-
cn/data/jj591617.aspx#PropertyIndex

您必须添加对以下内容的引用:

using System.Data.Entity.Infrastructure.Annotations;

基本范例

这是一个简单的用法,在

User.FirstName
属性上添加索引

modelBuilder     .Entity<User>()     .Property(t => t.FirstName)     .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute()));

实际示例:

这是一个更现实的例子。它将在多个属性上添加 唯一索引

User.FirstName
User.LastName
,索引名称为“
IX_FirstNameLastName”

modelBuilder     .Entity<User>()     .Property(t => t.FirstName)     .IsRequired()    .HasMaxLength(60)    .HasColumnAnnotation(        IndexAnnotation.AnnotationName,         new IndexAnnotation( new IndexAttribute("IX_FirstNameLastName", 1) { IsUnique = true }));modelBuilder     .Entity<User>()     .Property(t => t.LastName)     .IsRequired()    .HasMaxLength(60)    .HasColumnAnnotation(        IndexAnnotation.AnnotationName,         new IndexAnnotation( new IndexAttribute("IX_FirstNameLastName", 2) { IsUnique = true }));


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/573230.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号