如何在ASP.NET Boilerplate中使复合键唯一? [英] How to make composite unique key in ASP.NET Boilerplate?

查看:83
本文介绍了如何在ASP.NET Boilerplate中使复合键唯一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个以Id作为主键的表,我想在CodeValue列上有一个复合唯一键.我正在尝试这种方式.

I have a table which has the Id as primary key, I want to have a composite unique key on Code and Value column. I'm trying in this way.

[Table("Test")]

public class Test: FullAuditedEntity

{


[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public virtual int Code { get; set; }

[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public virtual int Value { get; set; }

但这使复合主键不是唯一键.

But it's making the composite primary key not unique key.

推荐答案

为其他人添加解决方案.

Adding solution for others.

不做任何修改,只需添加以下行

Don't modify anything just add the below line

modelBuilder.Entity<Test>(b =>
        {
            b.HasIndex(e => new { e.Code, e.Value}).IsUnique();
        });

这篇关于如何在ASP.NET Boilerplate中使复合键唯一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆