0

I want to change the following use of SqlParameter into a more typed version

private static void Update(int id, string lname)
{
  new RememberEntities().ExecuteStoreCommand(
    @"    UPDATE Users
          SET lname = @lname 
          WHERE Id = @id",
    new SqlParameter("lname", lname), new SqlParameter("id", id));
}

For example, I want to be able to write something a long the lines of:

private static void Update(int id, string lname)
{
  new RememberEntities().ExecuteStoreCommand(
    @"    UPDATE Users
          SET lname = @lname 
          WHERE Id = @id",
    new SqlParameterString("lname", lname), new SqlParameterInt("id", id));
}

Where the second parameter to SqlParameterString() would take a string and an int in the case of SqlParameterInt(). I've tried creating my own by subclassing SqlParameter, alas, it is sealed so no luck there.

I was wondering what is the recommended EF apprach?

2 Answers 2

2

You can create typed factory methods and use only them, but you really can't avoid using SqlParameter constructor directly (you'll have this problem even if SqlParameter would be not sealed, you can only create your own wrapper method in such case).

Sign up to request clarification or add additional context in comments.

1 Comment

Yes you are correct, factory methods could do the trick. I was hoping for a better alternative :)
1

You could use a stored procedure (that returns a scalar value) then using EF 4.0 you can add a 'Function Import' and then you can execute the stored procedure like any C# function.

1 Comment

Good idea, although I loathe stored procedures :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.