Skip to content

Commit

Permalink
Merge branch 'release/v0.3.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
verdie-g committed Mar 2, 2018
2 parents 58263ed + 71cce57 commit 08cbdbb
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<Build Include="dbo\Stored Procedures\ListAll.sql" />
<Build Include="dbo\Stored Procedures\Empty.sql" />
<Build Include="dbo\Stored Procedures\ListNotAll.sql" />
<Build Include="dbo\Stored Procedures\SelectParam.sql" />
</ItemGroup>
<ItemGroup>
<None Include="dbo\Scripts\Populate.sql" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE PROCEDURE [dbo].[SelectParam]
@n INT
AS
BEGIN
SELECT @n;
END
5 changes: 5 additions & 0 deletions StoredProcedureEFCore.Tests.SqlServer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ await ctx.LoadStoredProc("dbo.ListAll")
// Limit is omitted, it takes default value specified in the stored procedure
ctx.LoadStoredProc("dbo.ListAll")
.Exec(r => rows = r.ToList<Model>());

// EXEC dbo.SelectParam @n = NULL
await ctx.LoadStoredProc("dbo.SelectParam")
.AddParam<int?>("n", null)
.ExecScalarAsync<int?>(i => Console.WriteLine(i));
}
}
}
13 changes: 10 additions & 3 deletions StoredProcedureEFCore/StoredProcBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ public void ExecScalar<T>(out T val)
try
{
OpenConnection();
val = (T)_cmd.ExecuteScalar();
object scalar = _cmd.ExecuteScalar();
val = DefaultIfDBNull<T>(scalar);
}
finally
{
Expand All @@ -128,7 +129,8 @@ public async Task ExecScalarAsync<T>(Action<T> action)
try
{
await OpenConnectionAsync();
T val = (T)await _cmd.ExecuteScalarAsync();
object scalar = await _cmd.ExecuteScalarAsync();
T val = DefaultIfDBNull<T>(scalar);
action(val);
}
finally
Expand Down Expand Up @@ -156,7 +158,7 @@ private DbParameter AddParamInner<T>(string name, T val, ParameterDirection dire

DbParameter param = _cmd.CreateParameter();
param.ParameterName = name;
param.Value = val;
param.Value = (object)val ?? DBNull.Value;
param.Direction = direction;
param.DbType = DbTypeConverter.ConvertToDbType<T>();

Expand All @@ -173,5 +175,10 @@ private Task OpenConnectionAsync()
{
return _cmd.Connection.OpenAsync();
}

private T DefaultIfDBNull<T>(object o)
{
return o == DBNull.Value ? default(T) : (T)o;
}
}
}
2 changes: 1 addition & 1 deletion StoredProcedureEFCore/StoredProcedureEFCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>netstandard1.3</TargetFramework>
<Title>StoredProcedureEFCore</Title>
<Version>0.3.3</Version>
<Version>0.3.4</Version>
<Authors>Grégoire Verdier</Authors>
<Description>Entity Framework Core extension to execute a stored procedure and map the result</Description>
<RepositoryUrl>https://github.com/verdie-g/StoredProcedureEFCore</RepositoryUrl>
Expand Down

0 comments on commit 08cbdbb

Please sign in to comment.