I'm using Entity Framework 5, Code first against a SQL 2005 database. I have a repository, with a method which executes a stored procedure- The method looks like this;
public IEnumerable<PossibleDuplicateCustomer> GetPossibleDuplicates(Customer customer)
{
return DbContext.Database.SqlQuery<PossibleDuplicateCustomer>(
"EXEC SearchPotentialDuplicates @CustomerId = {0}, @FirstName = {1}, @LastName = {2}, @dob = {3}",
customer.CustomerId,
customer.CustomerFirstName,
customer.CustomerLastName,
customer.Dob);
}
Another variant I tried is;
public IEnumerable<PossibleDuplicateCustomer> GetPossibleDuplicates(Customer customer)
{
return DbContext.Database.SqlQuery<PossibleDuplicateCustomer>(
"SearchPotentialDuplicates @CustomerId, @FirstName, @LastName, @dob",
new SqlParameter("CustomerId", customer.CustomerId),
new SqlParameter("FirstName", customer.CustomerFirstName),
new SqlParameter("LastName", customer.CustomerLastName),
new SqlParameter("dob", customer.Dob));
}
When I execute this- i receive an error;
System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near 'SearchPotentialDuplicates'.
So i grabbed the generated sql using miniprofiler- which gave me;
DECLARE @p0 int = 12644,
@p1 nvarchar(4) = N'adam',
@p2 nvarchar(3) = N'ant',
@p3 datetime = '1951-11-01T00:00:00'
EXEC SearchPotentialDuplicates @CustomerId = @p0, @FirstName = @p1, @LastName = @p2, @dob = @p3
I tried copying and pasting this into ssms and it gave an error because the syntax of declaring and assigning in one line is not supported
Msg 139, Level 15, State 1, Line 0
Cannot assign a default value to a local variable.
Msg 137, Level 15, State 2, Line 6
Must declare the scalar variable "@p0".
This is a new sql 2008 thing (http://blogs.microsoft.co.il/blogs/bursteg/archive/2007/12/05/sql-server-2008-t-sql-declare-and-set-in-the-same-statement.aspx) and not support in SQL 2005- Changing the query to;
DECLARE @CustomerId int ,
@FirstName nvarchar(25),
@LastName nvarchar(25) ,
@dob datetime
SET @CustomerId = 12645
SET @FirstName = N'adam'
SET @LastName = N'ant'
SET @dob = '1951-11-01T00:00:00'
exec SearchPotentialDuplicates @CustomerId, @FirstName, @LastName, @dob
works fine! SO my question is how come entity framework is using this mental syntax? I've done some googling and people talk about ProviderManifestToken but this apparently is only needed if going to a totally different platform like sql ce, not between sql versions. So is there a setting I'm missing, or can i change the query to force it to execute in a different way?
Thanks All!