Michael Michael - 1 month ago 9
C# Question

How to make work SqlQuery<> method?

I use entity framework 6 in my project.

I use SqlQuery method of the Database class to get some string value from table.

Here is the code:

string name = "Dana";

context.Database.SqlQuery<string>("SELECT DisplayName FROM Users WHERE Username = name ", name).ToList();


But it seems that I use name variable inside
SqlQuery
method wrong.

Any idea what should I fix in my
SqlQuery
method string to query work properly?

Answer
context.Database.SqlQuery<string>("SELECT DisplayName FROM Users WHERE Username = @p0", name).ToList();

Any parameter values you supply will automatically be converted to a DbParameter.

See Database.SqlQuery Method