如何在对不同类型的属性创建唯一约束时解决错误 'No best type found for implicitly-typed array'?

How can I resolve error 'No best type found for implicitly-typed array' while creating unique constraint on properties of different types?

问题:使用 EF Core (v5),如何解决编译器错误 'No best type found for implicitly-typed array' 以允许我在多个数据库上创建唯一的数据库约束不同类型的属性?

我正在使用这样的代码:

modelBuilder.Entity<Entity>().HasAlternateKey(entity => new [] {entity.COL1, entity.COL2}).HasName("UniqueCOL1_COL2");

// example sourced from: https://docs.microsoft.com/en-us/answers/questions/187856/code-first-unique-constraint-on-multiple-columns.html#answer-190078

在我的例子中,我看到编译器错误 No best type found for implicitly-typed array,因为 COL1COL2 是不同的类型(在本例中是字符串和整数)。

This SO post 解释了错误的原因,但没有解释如何解决我的问题突出显示的问题。

我尝试使用一组对象,但没有用:

new object[] {entity.COL1, entity.COL2}

我能够使用匿名对象解决此问题。例如:

builder.HasAlternateKey(e => new { e.SomeInt, e.SomeString })