【问题标题】:Using a HashSet for foreign key objects in ServiceStack OrmLite with SQL Server在 SQL Server 的 ServiceStack OrmLite 中为外键对象使用 HashSet
【发布时间】:2015-12-09 01:59:43
【问题描述】:

我不得不使用关系对象解决多对多数据库设计,但我需要确保没有重复。我曾希望我可以将相关对象的集合定义为 HashSet,但 ServiceStack 不喜欢它。这是我用于 OrmLite 的 POCO 的简化版本:

public class Foo
{
    public int Id { get; set; }
    [Reference]
    public HashSet<FooToBar> FooToBars { get; set; }
}

public class Bar
{
    public int Id { get; set; }
    public string Name { get; set; }
}

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

    [References(typeof(Foo))]
    public int FooId { get; set; }
    [References(typeof(Bar))]
    public int BarId { get; set; }

    // This is the requirement that makes it a little odd
    public string Option { get; set; }
}

Foo 只能有唯一的Bars 和与之相关的Options。从 C# 的角度来看,HashSet 对此非常有用,但 ServiceStack 正在寻找 HashSet 类型上的相关实体 ID。

我有什么方法可以做到这一点不会让 ServiceStack 发疯吗?

【问题讨论】:

    标签: c# servicestack ormlite-servicestack


    【解决方案1】:

    引用类型属性需要是一个List,你仍然可以有HashSet属性但它不能是一个引用属性,即它会被POCO blobbed。

    【讨论】:

    • HashSet 作为参考也应该很棒,你不觉得吗?
    • @labilbe HashSet 是无序的,并且为了断言相等,每种类型都需要覆盖可能很少使用和错误来源的相等成员。在保存之前断言 List 唯一性更容易。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    • 2021-11-03
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多