为什么实体框架为使用NUMERIC(19)定义的列传递参数DECIMAL(5,0)? [英] Why is Entity Framework passing a parameter as DECIMAL(5,0) for a column defined with NUMERIC(19)?
问题描述
在SQL Server中,数据库列定义为NUMERIC(19).在< edmx:StorageModels>中,edmx显示:
A database column is defined as NUMERIC(19) in SQL Server. In <edmx:StorageModels>, the edmx shows:
<Property Name="supplier_id" Type="numeric" Nullable="false" Precision="19" />
在< edmx:ConceptualModels>中,我将其视为:
In <edmx:ConceptualModels>, I see it as:
<Property Type="Decimal" Name="supplier_id" Nullable="false" Precision="19" Scale="0" />
在数据库中,带有WHERE子句中此列的SELECT带有以下参数定义:
In the database, a SELECT with this column in the WHERE clause comes through with this parameter definition:
(@p__linq__0 decimal(5,0))
此WHERE子句:
WHERE ([Extent1].[supplier_id] = @p__linq__0)
我知道数字和十进制是等效的,但是为什么精度会改变?
I understand that numeric and decimal are equivalent, but why is the precision getting changed?
推荐答案
(@p__linq__0 decimal(5,0))
是您提供的参数值的精度.
(@p__linq__0 decimal(5,0))
is the Precision of the parameter value your provided.
例如,
decimal number = 12345m;
var result = context.MyTables.Where(x => x.Number == number).ToList();
然后查询将像这样-
exec sp_executesql N'SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Number] AS [Number]
FROM [dbo].[MyTable] AS [Extent1]
WHERE [Extent1].[Number] = @p__linq__0',N'@p__linq__0 decimal(5,0)',
@p__linq__0=12345
答案是 where子句的SQL语句不需要相同的精度编号.
The answer is SQL Statement doesn't need to be same Precision number for where clause.
实体框架足够聪明,可以找出数字的精度,并且仅创建具有所需精度的查询.
Entity Framework is smart enough to figure out the Precision of a number, and only creates the query with required Precision.
注意:插入和更新不是这种情况; EF创建的查询与列的精度相同.
Note: it is not the case for Insert and Update; EF creates a query with the same Precision as Column.
这篇关于为什么实体框架为使用NUMERIC(19)定义的列传递参数DECIMAL(5,0)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!