【问题标题】:Columns with data-type prefix cause "Type X is not a defined system type" error具有数据类型前缀的列导致“类型 X 不是已定义的系统类型”错误
【发布时间】:2018-12-19 04:50:09
【问题描述】:

我正在使用第 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,发现这个专栏(以及其他一些专栏)使用了 stage 数据类型:

with stage 我的意思是它们的前缀就像这里的 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 选项来解决此问题?

【问题讨论】:

  • 请参阅this 答案以获得解决方法。

标签: c# tsql entity-framework-core sql-server-2014


【解决方案1】:

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

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

你是怎么落到这种境地的,我说不上来。我认识的大多数人(我很欣赏这只是意见)避开 SQL Server 中的用户定义类型(其他用户定义的 table 类型),因为它们只开始工作的一半。1


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

【讨论】:

  • 哦,这可以解释很多。然后我将不得不在谷歌上搜索如何使 ef-core 与这些类型一起工作......否则我可能会自己修复生成的HasColumnType 的所有出现:-(
猜你喜欢
  • 2020-09-10
  • 1970-01-01
  • 2021-01-06
  • 1970-01-01
  • 1970-01-01
  • 2012-09-12
  • 1970-01-01
  • 1970-01-01
  • 2017-09-02
相关资源
最近更新 更多