Servicestack Ormlite 多列约束失败,其中约束包括枚举

Servicestack Ormlite multi-column constraint fails where constraint includes Enum

我正在使用 ServiceStack.Ormlite,并且还大量使用枚举的自动处理功能,因此它们作为字符串存储在数据库中,但在检索时可以很好地检索并解析回枚举,所以我可以轻松完成类型比较 - 例如,对于枚举类型 "UserRoleEnum" 的 db/table class "User" 中的 属性 "UserRole"(仅用于演示)。

这很好..直到我想使用枚举 属性 来定义多列唯一约束

CompositeIndexAttribute(bool unique, params string[] fieldNames);

喜欢:

[CompositeIndex(true, nameof(UserId), nameof(UserRole)]
public class User 
{ 
    public long UserId {get;set;} 
    public UserRoleEnum UserRole {get;set; 
}

(根据: How to Create Unique Constraint with Multiple Columns using ServiceStack.OrmLite?).

此时我得到:

System.Data.SqlClient.SqlException
Column 'UserRole' in table 'User' is of a type that is invalid for use as a key column in an index.

我目前看到的选项是:

a) 在 table 实体 class 中将 UserRole 定义为字符串(不是 UserRoleEnum 的 ntead)并失去 Enum 使用....每次都必须手动测试该值以确认db 值是我稍后在业务逻辑中期望的值

b) 继续使用 UserRoleEnum 但失去了使用 class 属性声明多列唯一约束的能力,并且可能必须使用后续的数据库迁移脚本手动创建它们?

有什么方法可以开箱即用地使枚举和多列约束很好地发挥作用吗?

这个问题是因为 enum 属性使用 VARCHAR(MAX) 的默认字符串定义回退,SQL 服务器不允许您在其上创建索引,而 VARCHAR(MAX) 的列定义 string 属性 是 VARCHAR(8000)

此问题现已从 this commit which now uses the VARCHAR(255) string definition of the EnumConverter Type Converter. This change is available from v4.5.5 that's now available on MyGet 解决。

否则,您还可以通过添加 [StringLength] 属性来更改列定义的大小以匹配 string 属性,例如:

[CompositeIndex(true, nameof(UserId), nameof(UserRole))]
public class User
{
    public long UserId { get; set; }
    [StringLength(8000)]
    public string UserRole { get; set; }
}