Skip to content

Commit fc7f360

Browse files
committed
Update test project
1 parent b68cba2 commit fc7f360

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

NHUnitExample/Controllers/TestController.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public async Task<IActionResult> InitializeDatabase(CancellationToken token)
7272
{
7373
Name = "Product " + i,
7474
Colors = new List<Color> { colors.ElementAt(random.Next(0, colors.Count - 1)) },
75-
Price = random.Next(100, 10000)/100m
75+
Price = random.Next(100, 10000) / 100m
7676
}
7777
).ToList();
7878
await _dbContext.Products.InsertManyAsync(products, token);
@@ -235,5 +235,24 @@ public async Task<IActionResult> TestMultipleQueries(CancellationToken token)
235235
Top10ExpensiveProducts = expensiveProducts,
236236
});
237237
}
238+
239+
[HttpGet("/testSqlQuery")]
240+
public async Task<IActionResult> TestSqlQuery(CancellationToken token, int customerId = 11)
241+
{
242+
var sqlQuery = @"select Id as CustomerId,
243+
Concat(FirstName,' ',LastName) as FullName,
244+
BirthDate
245+
from ""Customer""
246+
where Id= :customerId";
247+
var customResult = await _dbContext.ExecuteScalarAsync<SqlQueryCustomResult>(sqlQuery, new { customerId }, token);
248+
return Ok(customResult);
249+
}
250+
class SqlQueryCustomResult
251+
{
252+
public SqlQueryCustomResult() { }
253+
public int CustomerId { get; set; }
254+
public string FullName { get; set; }
255+
public DateTime BirthDate { get; set; }
256+
}
238257
}
239258
}

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,12 @@ In some rare cases you need to execute your own queries and NHUnit provides this
7878
- ExecuteListAsync
7979
- ExecuteScalarAsync
8080
- ExecuteNonQueryAsync
81+
82+
```csharp
83+
var sqlQuery = @"select Id as CustomerId,
84+
Concat(FirstName,' ',LastName) as FullName,
85+
BirthDate
86+
from ""Customer""
87+
where Id= :customerId";
88+
var customResult = await _dbContext.ExecuteScalarAsync<SqlQueryCustomResult>(sqlQuery, new { customerId = 11 });
89+
```

src/NHUnit/UnitOfWork.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ protected IQuery CreateQueryWithParameters(string queryString, object parameters
193193
if (parameters != null)
194194
{
195195
var properties = parameters.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty);
196+
if (!properties.Any())
197+
{
198+
throw new ArgumentException("Object with public properties expected for query creation", nameof(parameters));
199+
}
196200
foreach (var property in properties)
197201
{
198202
var name = property.Name;

0 commit comments

Comments
 (0)