1

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 ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
NSS
  • 1,835
  • 2
  • 29
  • 66
  • 1
    Try `"EXEC [ABC.Security.GetUserLocation] {0}"` – dmeglio Aug 03 '15 at 19:53
  • possible duplicate of [How to call Stored Procedure in Entity Framework 6 (Code-First)?](http://stackoverflow.com/questions/20901419/how-to-call-stored-procedure-in-entity-framework-6-code-first) – laylarenee Aug 03 '15 at 20:42

2 Answers2

1

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

Community
  • 1
  • 1
Abiezer
  • 772
  • 8
  • 14
1

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.

Scharly Ochoa
  • 301
  • 1
  • 6
  • The first parameter need to be string value(or literal value) and not an SQLParameter at for SQL 2005 Version. – NSS Aug 03 '15 at 22:16