Friday, April 15, 2011

How to generate Entity Framework or Linq To SQL wrappers for system stored procedures?

Title says it all. I want to be able to work with system stored procedures (sp_helpXXX etc) through generated Linq-To-SQL or Entity Framework wrappers. Problem is, system sprocs are not listed in the generation wizards. I have also tried running sqlmetal.exe manually, but no system stored procedures show up.

From stackoverflow
  • I don't know if there is automatic way, but in linq2sql you can define stored procedures and function by hand on the partial class of the data context.

    Here is one I use for the sql's GetDate function (which I can use in the linq query expressions):

    [Function(Name = "GetDate", IsComposable = true)]
    public DateTime GetSystemDate()
    {
        MethodInfo mi = MethodBase.GetCurrentMethod() as MethodInfo;
        return (DateTime)this.ExecuteMethodCall(this, mi, new object[] { }).ReturnValue;
    }
    

    The same principle applies for Stored Procedure (add a regular stored procedure through the designer, and check it out in the generated code).

0 comments:

Post a Comment