NSS
NSS

Reputation: 2022

Execute stored procedure using Entity Framework using LINQ

I am getting

Incorrect syntax near 'ABC.Security.GetUserLocation'.

when executing a stored procedure with the following code.

. dots are the part of Name (for grouping), don't confuse it with Schema.

Something like "FirstName.LastName". I can execute it from SSMS without any problem.

var userNameToSearch = new SqlParameter("@userName", userName);
userNameToSearch.DbType = System.Data.DbType.String;
userNameToSearch.Size = 100;

List<Location> locations = db.Database.SqlQuery<Location>(@"[ABC.Security.GetUserLocation]", userNameToSearch).ToList();

return locations;

What would be the right way to use names like that from C# code ?

Upvotes: 1

Views: 3677

Answers (2)

Scharly Ochoa
Scharly Ochoa

Reputation: 301

Try to be explicit with your parameters

List<Location> locations = db.Database.SqlQuery<Location>(@"exec [ABC.Security.GetUserLocation] {0}", userNameToSearch).ToList();

"{0}" means that you are passing userNameToSearch as first parameter, doesnt matter whats the parameter's name.

Upvotes: 1

Abiezer
Abiezer

Reputation: 782

You can call a stored procedure in your DbContext class as follows.

this.Database.SqlQuery<YourEntityType>("storedProcedureName",params);

here more information : https://msdn.microsoft.com/en-us/data/jj691402.aspx

this was an answer from this question : here

Upvotes: 1

Related Questions