C# 如何使用实体框架为可空列设置唯一约束?

C# 如何使用实体框架为可空列设置唯一约束?,c#,sql-server,entity-framework,C#,Sql Server,Entity Framework,我使用的是实体框架版本6,我的模型如下: public class SizeCount { public int Count { get; set; } public Size Size { get; set; } public long? SizeId { get; set; } public Color Color { get; set; } public long? ColorId { get; set

我使用的是实体框架版本6,我的模型如下:

public class SizeCount
    {
        public int Count { get; set; }
        public Size Size { get; set; }
        public long? SizeId { get; set; }
        public Color Color { get; set; }
        public long? ColorId { get; set; }
        public Product Product { get; set; }
        public long ProductId { get; set; }
    }
我想防止
ColorId
SizeId
都为空,我想
ProductId
ColorId
SizeId
是唯一的。 一些示例记录:

ProductId SizeId ColorId
1         null   1       > allow
1         null   1       > not allow
1         1      null    > not allow
2         1      null    > allow
2         null   1       > not allow
SizeId
ColorId
可以为空。
在实体框架中是否有任何属性可以帮助我,或者我应该手动检查它?

希望这有帮助,这是一个稍微修改的版本。对于SizeCount类-

using System.ComponentModel.DataAnnotations.Schema;

public class SizeCount
{
    public int Id { get; set; }
    public int Count { get; set; }

    [Index("IX_SizeUnique", 1, IsUnique = true)]
    public int SizeId { get; set; }
    public virtual Size Size { get; set; }

     [Index("IX_ColorUnique", 1, IsUnique = true)]
    public int ColorId { get; set; }
    public virtual Color Color { get; set; }

     [Index("IX_ProductUnique", 1, IsUnique = true)]
    public int ProductId { get; set; }
    public virtual Product Product { get; set; }

}

public class Product
    {
        public int Id { get; set; }
    }

    public class Color
    {
        public int Id { get; set; }
    }

    public class Size
    {
        public int Id { get; set; }
    }
上下文

public class TestDbContext: DbContext
    {
        public DbSet<SizeCount> SizeCounts { get; set; }
        public DbSet<Product> Products { get; set; }
        public DbSet<Size> Sizes { get; set; }
        public DbSet<Color> Colors { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
           base.OnModelCreating(modelBuilder);
        }
    }

具有可为空列的唯一约束

如果其他任何人试图用fluent API db上下文做类似的事情,这对我来说是有效的

modelBuilder.Entity<AppointmentRequest>()
    .HasIndex(r => new { r.CustomerId, r.StartAt })
    .HasFilter("StartAt IS NOT NULL")
    .IsUnique();

modelBuilder.Entity<AppointmentRequest>()
    .HasIndex(r => r.CustomerId)
    .HasFilter("StartAt IS NULL")
    .IsUnique();
modelBuilder.Entity()
.HasIndex(r=>new{r.CustomerId,r.StartAt})
.HasFilter(“StartAt不为空”)
.IsUnique();
modelBuilder.Entity()
.HasIndex(r=>r.CustomerId)
.HasFilter(“StartAt为空”)
.IsUnique();

它有效地将null视为一个值。

那么,简单地说,您想将
null
视为一个值吗?也就是说,每列只能使用一次?为什么你不允许第三排,但允许第四排?是的@JᴀʏMᴇᴇ 如果可能的话,我认为您正在寻找带有where子句的唯一约束。这是否可以实现取决于您使用的SQL版本。是2008年还是更高版本?我的sql server是2012年。为什么此
1空>不允许
是不允许的?
modelBuilder.Entity<AppointmentRequest>()
    .HasIndex(r => new { r.CustomerId, r.StartAt })
    .HasFilter("StartAt IS NOT NULL")
    .IsUnique();

modelBuilder.Entity<AppointmentRequest>()
    .HasIndex(r => r.CustomerId)
    .HasFilter("StartAt IS NULL")
    .IsUnique();