-
Notifications
You must be signed in to change notification settings - Fork 3
Fluent API
abe545 edited this page Oct 29, 2014
·
3 revisions
You can call the Stored Procedure using a fluent syntax. You start with the StoredProcedure.Create method:
public class MyDataBase : DbContext
{
public void CallMyStoredProc()
{
StoredProcedure.Create("MyStoredProc")
.Execute(this.Database.Connection);
}
public Task CallMyStoredProcAsync()
{
return StoredProcedure.Create("schema", "MyStoredProc")
.ExecuteAsync(this.Database.Connection);
}
}If your procedure returns one or more result sets (up to 7), you use the WithResults method:
public class MyDataBase : DbContext
{
public Tuple<IEnumerable<Results1>, IEnumerable<Results2>> CallMyStoredProc()
{
return StoredProcedure.Create("MyStoredProc")
.WithResults<Results1, Results2>()
.Execute(this.Database.Connection);
}
public Task<IEnumerable<Results>> CallMyStoredProcAsync()
{
return StoredProcedure.Create("schema", "MyStoredProc")
.WithResults<Results>()
.ExecuteAsync(this.Database.Connection);
}
}You can pass input parameters (and retrieve output parameters or return values) with either of the methods shown in Passing Parameters.
Sometimes the data returned from the Database isn't exactly what you'd want to use in your model. You can massage the data returned by passing an instance of IDataTransformer:
public class ToUpperTransformer : IDataTransformer
{
public bool CanTransform(object value, Type targetType, IEnumerable<Attribute> propertyAttributes)
{
return value is string;
}
public object Transform(object value, Type targetType, IEnumerable<Attribute> propertyAttributes)
{
return ((string)value).ToUpper();
}
}
StoredProcedure.Create("dbo", "MyStoredProc")
.WithDataTransformer(new ToUpperTransformer());