Entity Framework Core 2.1 System.Data.SqlClient.SqlException (0x80131904): Type Flag is not a defined system type

0

After upgrading to EntityFramework 2.1.11, I am facing the following issue.

System.Data.SqlClient.SqlException (0x80131904): Type Flag is not a defined system type.

I am getting this Error for Linq to SQL internal translation. 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.

The reason is column == 1 is translated as CAST(1 AS Flag)internally in 2.1 which was working in 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.

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:

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:

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. Could anyone please help me on this?

Added additional information based on comments from David. 1) I haven't created any custom type for this and not missing. 2) C# datat type is Byte for pl.Primary and pl.Deleted. 3) In the dbContext I am seeing the following in onModelCreating method. entity.Property(e => e.Primary).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.

entity-framework-core-2.1
entity-framework-core-migrations
asked on Stack Overflow Jun 17, 2019 by Rajeevan • edited Jun 18, 2019 by Rajeevan

1 Answer

0

The problem is that you have HasColumnType("Flag") in the configuration for your properties. This tells Entity Framework that the type of the column is Flag, obviously not a standard SQL Server data type. 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. For example, your table would look something like this:

CREATE TABLE Products
(
    -- Other columns
    Primary BIT,
    Deleted BIT
)

and your C# class like this

public class Product
{
    // Snip other columns
    public bool Primary { get; set; }
    public bool Deleted { get; set; }
}
answered on Stack Overflow Jun 17, 2019 by DavidG

User contributions licensed under CC BY-SA 3.0