开发者

Typed SqlParameter's in Entity Framework

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?


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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜