在带有 SQL 服务器的 ServiceStack OrmLite 中为外键对象使用 HashSet
Using a HashSet for foreign key objects in ServiceStack OrmLite with SQL Server
我不得不使用关系对象解决多对多数据库设计问题,但我需要确保没有重复项。我曾希望我可以将相关对象的集合定义为一个 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; }
}
Class Foo
只能有唯一的 Bar
与 Option
相关。从 C# 的角度来看,HashSet
非常适合此操作,但 ServiceStack 正在寻找 HashSet
类型的相关实体 ID。
有什么方法可以做到这一点而不会使 ServiceStack 崩溃吗?
引用类型属性需要是一个 List,你仍然可以有 HashSet 属性,但它不能是一个引用 属性,即它会被 POCO 弄得一团糟。
我不得不使用关系对象解决多对多数据库设计问题,但我需要确保没有重复项。我曾希望我可以将相关对象的集合定义为一个 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; }
}
Class Foo
只能有唯一的 Bar
与 Option
相关。从 C# 的角度来看,HashSet
非常适合此操作,但 ServiceStack 正在寻找 HashSet
类型的相关实体 ID。
有什么方法可以做到这一点而不会使 ServiceStack 崩溃吗?
引用类型属性需要是一个 List,你仍然可以有 HashSet 属性,但它不能是一个引用 属性,即它会被 POCO 弄得一团糟。