首页 > 解决方案 > 如何根据比较分组 ID 使列唯一

问题描述

我需要确保特定站点元素的所有视图数据都是唯一的,但我希望能够为具有不同 ID 的其他站点重用该值。

例子:

站点一

站点二

查看型号:

public class View : AssetsBase, IView
{
    public int SiteId { get; set; }
    public string Name { get; set; }
    public string Ref { get; set; }
    public virtual IEnumerable<MetaEntry> MetaEntries { get; set; } = new HashSet<MetaEntry>();
    public virtual IEnumerable<HreflangEntry> HreflangEntries { get; set; } = new HashSet<HreflangEntry>();
}

数据库上下文:

builder.Entity<View>().ToTable("SiteView").HasIndex(sw => sw.Ref).IsUnique();

当前方法按预期工作,但我仅限于使用一次 Ref 值。我想要的可能吗?

标签: c#databaseasp.net-coreentity-framework-core

解决方案


感谢提供解决方案的 user700390。

数据库上下文:

builder.Entity<View>().ToTable("SiteView").HasIndex(sw => new { sw.Ref, sw.SiteId }).IsUnique();

推荐阅读