[英]Entity Framework Core 2.1 System.Data.SqlClient.SqlException (0x80131904): Type Flag is not a defined system type
After upgrading to EntityFramework 2.1.11, I am facing the following issue. 升级到EntityFramework 2.1.11后,我面临以下问题。
System.Data.SqlClient.SqlException (0x80131904): Type Flag is not a defined system type.
I am getting this Error for Linq to SQL internal translation. 我收到Linq to SQL内部翻译的错误。 There are two columns in database table which are of tinyint datatype which have corresponding C# datatype as byte, which is throwing exception in Linq while querying.
数据库表中有两列为tinyint数据类型,其对应的C#数据类型为byte,在查询时在Linq中抛出异常。
The reason is column == 1
is translated as CAST(1 AS Flag)
internally in 2.1 which was working in 2.0. 原因是
column == 1
在2.1内部被翻译为CAST(1 AS Flag)
,其工作在2.0。
It is working if we change == 1
to == Convert.ToByte(1)
or assigning to byte variable and using that as ==
variable which I think is not the ideal fix. 如果我们
change == 1
为== Convert.ToByte(1)
或分配给字节变量并将其用作==
变量,我认为这不是理想的修复。
This is the piece of code which is throwing error. 这是抛出错误的代码片段。
var query = await (from pl in _azureContext.Product.AsNoTracking()
where pl.Primary ==1 && pl.Deleted == 0
select new Product
{
ProductId = pl.ProductId,
ProductName = pl.ProductName
}).OrderBy(P => P.ProductName).ToListAsync<Product>();
SQL Internal Translation which throws exception is as follows: 抛出异常的SQL内部翻译如下:
SELECT [pl].[ProductId] , [pl].[ProductName] FROM [Products] AS [pl] WHERE ([pl].[Primary] = CAST(1 AS Flag)) AND ([pl].[Deleted] = CAST(0 AS Flag)) ORDER BY [pl].[ProductName]
The Expected SQL Translation is as follows: 预期的SQL翻译如下:
SELECT [pl].[ProductId] , [pl].[ProductName] FROM [Products] AS [pl] WHERE ([pl].[Primary] = 1) AND ([pl].[Deleted] = 0) ORDER BY [pl].[ProductName]
It looks like a bug in Entityframework Core 2.1. 它看起来像Entityframework Core 2.1中的一个错误。 Could anyone please help me on this?
有人可以帮我这个吗?
Added additional information based on comments from David. 根据David的评论添加了其他信息。 1) I haven't created any custom type for this and not missing.
1)我没有为此创建任何自定义类型,也没有丢失。 2) C# datat type is Byte for pl.Primary and pl.Deleted.
2)C#数据类型是pl.Primary和pl.Deleted的字节。 3) In the dbContext I am seeing the following in onModelCreating method.
3)在dbContext中,我在onModelCreating方法中看到以下内容。 entity.Property(e => e.Primary).HasColumnType("Flag");
entity.Property(e => e.Primary).HasColumnType(“Flag”); entity.Property(e => e.Deleted).HasColumnType("Flag");
entity.Property(e => e.Deleted).HasColumnType(“Flag”);
Note: DbContext was generated earlier with .net core 2.0 and no code changes done on that. 注意:早期使用.net core 2.0生成了DbContext,并且没有对其进行任何代码更改。
The problem is that you have HasColumnType("Flag")
in the configuration for your properties. 问题是您的属性配置中有
HasColumnType("Flag")
。 This tells Entity Framework that the type of the column is Flag
, obviously not a standard SQL Server data type. 这告诉Entity Framework列的类型是
Flag
,显然不是标准的SQL Server数据类型。 The simple solution is to remove that configuration method. 简单的解决方案是删除该配置方法。
However, those columns are obviously meant to be boolean flags, and you should be using the appropriate data type. 但是,这些列显然是布尔标志,您应该使用适当的数据类型。 This means in C# your type is
bool
and in SQL Server it is bit
. 这意味着在C#中你的类型是
bool
,在SQL Server中它是bit
。 For example, your table would look something like this: 例如,您的表格看起来像这样:
CREATE TABLE Products
(
-- Other columns
Primary BIT,
Deleted BIT
)
and your C# class like this 和你的C#类一样
public class Product
{
// Snip other columns
public bool Primary { get; set; }
public bool Deleted { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.