sql-server - 使用 Entity Framework Fluent API 的带有 where 子句的唯一索引
问题描述
有没有办法使用实体框架指定多列唯一性约束?
这是我想要的约束
CREATE UNIQUE NONCLUSTERED INDEX myconstraint
ON myTable(col1,col2)
WHERE col2 IS NOT NULL;
下面的代码让我了解了 Entity Framework Fluent API 的大部分内容,但由于它不允许空值(即,两个空值col2
将被视为违反约束,而我希望它允许多个空值),所以它不够。
modelBuilder.Entity<myTable>().Property(x => x.col1)
.HasColumnAnnotation(IndexAnnotation.AnnotationName,
new IndexAnnotation(new IndexAttribute("myconstraint", 0) { IsUnique = true }));
modelBuilder.Entity<myTable>().Property(x => x.col2)
.HasColumnAnnotation(IndexAnnotation.AnnotationName,
new IndexAnnotation(new IndexAttribute("myconstraint", 1) { IsUnique = true }));
解决方案
推荐阅读
- amazon-web-services - 捕获选择中的值并使用 jq 在顶层对象中使用它
- react-native - 无法在 iOS 模拟器上运行 expo 项目
- asynchronous - 如何在 Rust 中组合两个异步函数
- git - Visual Studio 代码中的 Git 表示即使没有更改,文件也会被修改
- vim - Vim 中的 :terminal 颜色方案与 Vim 或主机终端不同
- autodesk-forge - Forge Viewer 的性能
- pandas - 多个条件 np.where
- android - Flutter - 如何从相机获取实时输入并扫描任何物体?
- python - 我如何让这个代码计算单词而不是字母?
- javascript - 在画布Angular中放大和缩小图像的问题