dotnet / ef6

This is the codebase for Entity Framework 6 (previously maintained at https://entityframework.codeplex.com). Entity Framework Core is maintained at https://github.com/dotnet/efcore.
https://docs.microsoft.com/ef/ef6
MIT License
1.43k stars 545 forks source link

User defined table inserting wrong value #1503

Closed JoostKingma closed 4 years ago

JoostKingma commented 4 years ago

When using the values (0.05 or 0.01) the query generator shows the values (5 or 1) when these are used within a SqlDbType.Structured SqlParameter.

I think this is best explained with code...

Currently the produced code is:

declare @p3  dbo.someUDT
insert into @p3 values(NULL,5)
insert into @p3 values(5,0.10)
insert into @p3 values(NULL,1)
insert into @p3 values(1,2)

exec sp_executesql N'Select * from @AName',N'@AName  [dbo].[someUDT] READONLY',@AName=@p3

While the correct code would be:

declare @p3  dbo.someUDT
insert into @p3 values(NULL,0.05)
insert into @p3 values(0.05,0.10)
insert into @p3 values(NULL,0.01)
insert into @p3 values(0.01,0.02)

exec sp_executesql N'Select * from @AName',N'@AName  [dbo].[someUDT] READONLY',@AName=@p3

Steps to reproduce

This is some standalone code to reproduce the issue

The User defined table definition:

CREATE TYPE [dbo].[someUDT] AS TABLE(
   [value1] [decimal](16, 5) NULL,
   [value2] [decimal](16, 5) NULL
)

some C# code, the context that is used isn't of importance for this issue.

DataTable dataTable = new DataTable();
dataTable .Columns.Add("value1", typeof(decimal));
dataTable .Columns.Add("value2", typeof(decimal));

dataTable .Rows.Add(null,0.05); 
dataTable .Rows.Add(0.05,0.1); 
dataTable .Rows.Add(null,0.01); 
dataTable .Rows.Add(0.01,0.02); 
List<SqlParameter> Parameters = new List<SqlParameter>();

Parameters.Add(new SqlParameter("@AName", SqlDbType.Structured) { Value = dataTable , TypeName= "dbo.someUDT" });

dbContext.Database.ExecuteSqlCommand("Select * from @AName",Parameters.ToArray());

Further technical details

EF version: 6.3 Database Provider: EntityFramework.SqlServer Operating system: Windows 10) IDE: Visual Studio 2017 15.6

JoostKingma commented 4 years ago

I made a stackoverflow question about this issue and the user "gert-arnold" figured it out. This issue is not related to entity-framework.

Based on his answer I found an existing bug report here: https://feedback.azure.com/forums/908035-sql-server/suggestions/38357785-sql-server-profiler-textdata-column-handles-decima

The stackoverflow question: https://stackoverflow.com/questions/59152654/user-defined-table-in-entity-framework-generating-incorrect-query