简体   繁体   English

实体框架核心2.1 System.Data.SqlClient.SqlException(0x80131904):类型标志不是已定义的系统类型

[英]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.

相关问题 如何将 null 参数传递给 Entity Framework Core 2.1 中的上下文查询类型 - How to pass null parameters to context query type in Entity Framework Core 2.1 获取DbContext Entity Framework Core 2.1中的实体模型列表 - Get List of Entity Models in DbContext Entity Framework Core 2.1 实体框架核心2.1:OnDelete级联-多个级联路径 - Entity Framework Core 2.1: OnDelete Cascade - multiple cascade paths Entity Framework Core 2.1 - FromSql 方法中的通用模型 - Entity Framework Core 2.1 - Generic Model in FromSql method Entity Framework Core 2.1 - 自有类型和嵌套值对象 - Entity Framework Core 2.1 - owned types and nested value objects 实体框架核心2.1上的脚手架无钥匙模型 - Scaffolding Keyless Models on entity framework core 2.1 实体框架核心 - EF Core 2.2 - &#39;Point.Boundary&#39;是一种接口类型(&#39;IGeometry&#39;) - Entity Framework Core - EF Core 2.2 - 'Point.Boundary' is of an interface type ('IGeometry') 无法添加实体类型“X”的种子实体,因为没有为所需属性“..ID”提供值 - The seed entity for entity type 'X' cannot be added because the was no value provided for the required property "..ID" 将String转换为linq条件实体框架核心 - Convert String to linq condition Entity Framework core 实体框架核心DBContext数据模型为变量 - Entity Framework Core DBContext Datamodel as variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM