Randy
Randy

Reputation: 1287

How to make Entityframework Core set the right Parameter Data Type

I have a User Model with this property defined:

[Column("custnmbr", TypeName = "char(15)")]
[StringLength(15)]
public string Custnmbr { get; set; }

I reference this in a query:

.FromSqlInterpolated($"SELECT * FROM [cp].[GetEcommerceItemsView] WHERE [CustomerNumber] = {user.Custnmbr}")

But when Entityframework Core Interpolates my parameter, it sets the Data Type to @p0 AS nvarchar(4000). That causes CONVERT_IMPLICIT in my query plan:

Type conversion in expression (CONVERT_IMPLICIT(nchar(15),[C].[CUSTNMBR],0)=[@p0]) may affect "SeekPlan" in query plan choice

My questions are: Should I be worried about this and if yes, How do I fix it?

Upvotes: 2

Views: 463

Answers (1)

Guru Stron
Guru Stron

Reputation: 142213

You can try switching to FromSqlRaw and creating SqlParameter with explicit type. Something like this:

var p1 = new SqlParameter("@Custnmbr", SqlDbType.Char, 15);
p1.Value = user.Custnmbr;
(...)
.FromSqlRaw($"SELECT * FROM [cp].[GetEcommerceItemsView] WHERE [CustomerNumber] = @Custnmbr", p1);

Upvotes: 2

Related Questions