具有数据类型前缀的列导致 "Type X is not a defined system type" 错误

Columns with data-type prefix cause "Type X is not a defined system type" error

我正在使用第 3 方数据库。通常,我在 LINQPad 中编写并随后转换为 Entity Framework Core (2.1) 的查询可以正常工作。然而,这次不是。

当我尝试使用列 Status 时,我得到以下 SqlException:

Class         16  byte
LineNumber    1   int
Message       "Type Enumeration is not a defined system type."    string
Number        243 int
Procedure     ""  string
Server        "..."   string
Source        ".Net SqlClient Data Provider"  string
State         2   byte

所以我打开了 SQL Server Management Studio 并发现该专栏(以及其他一些专栏)使用 strage 数据类型:

with strage 我的意思是它们像这里一样以 EnumerationCurrencyCode:varchar(3) 等为前缀

我使用 EF-Core 脚手架创建实体,属性 定义为:

public short Status { get; set; }

看起来不错,但 EF-Core 引擎显然识别了 Enumeration 前缀并在 OnModelCreating 方法中生成了这一行:

entity.Property(e => e.Status).HasColumnType("Enumeration");

我想这是导致错误的原因。


我以前从未见过这种情况,我想知道为什么 EF-Core 无法查询此类列而 LINQPand 却没有,这些奇怪的数据类型是什么?

EF-Core 是否有 magicsecret 选项来解决这个问题?

为什么会这样显示是因为您使用的是用户定义的数据类型。此脚本重现了该问题:

create type Enumeration from smallint null
go
create table T (
    ID int not null,
    E Enumeration not null
)

你是怎么落到这种境地的,我说不上来。我认识的大多数人(我很感激这只是意见)回避 SQL 服务器中的用户定义类型(其他用户定义的 table 类型)因为他们刚开始只做了一半。1


1例如用户定义的 table 类型的一个有用功能是,如果您可以自动应用特定约束 - 一个只能落在 1-100 范围内的数字。您不能使用 UDT 系统执行此操作。您可以单独创建 RULE 并将它们绑定到列,但您现在将此定义分布在多个位置 规则无论如何都已弃用。