Skip to content
abe545 edited this page Oct 29, 2014 · 3 revisions

#Code Only Stored Procedures

Code Only Stored Procedures will not create any Stored Procedures on your database. Instead, its aim is to make it easy to call your existing stored procedures by writing simple code.

This library is released via NuGet. You can go to the CodeOnlyStoredProcedures NuGet Page for more information on releases, or just grab it:

Install-Package CodeOnlyStoredProcedures

Quick Start

public class Person
{
    [Column("ResultId")]
    public int Id { get; set; }
    public DateTime Birthday { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    [NotMapped]
    public string FullName { get { return FirstName + " " + LastName; } }
}

public class MyModel : DbContext
{
    private readonly StoredProcedure<Person> peopleByBirthday = 
        new StoredProcedure<Person>("dbo", "usp_GetPeopleByBirthday");

    public IEnumerable<Person> GetPeopleByBirthday(DateTime birthday)
    {
        return peopleByBirthday.WithInput(new { Birthday = birthday })
            .Execute(this.Database.Connection);
    }

    public Task<IEnumerable<Person>> GetPeopleByLastName(string lastName)
    {
        // schema defaults to "dbo"
        return StoredProcedure.Create("usp_GetPeopleByFamilyName")
            .WithInput("familyName", lastName)
            .WithResult<Person>()
            .Execute(this.Database.Connection);
    }
}

Dynamic Syntax

The new dynamic syntax in 1.2 makes calling your stored procedures a LOT easier

public class DynamicModel : DbContext
{
    // Just by specifying the result type and parameters, we are able to figure out
    // how to call the query.
    public IEnumerable<Person> GetPeopleByBirthday(DateTime birthday)
    {
        return Database.Connection.Execute().usp_GetPeopleByBirthday(Birthday: birthday);
    }

    // If you specify the result as a Task, the procedure will be executed asynchronously
    public Task<IEnumerable<Person>> GetPeopleByLastName(string lastName)
    {
        return Database.Connection.ExecuteAsync().usp_GetPeopleByFamilyName(familyName: lastName);
    }
}
Clone this wiki locally