1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
#if !NET40
namespace System.Data.Entity.EntityClient
{
using System.Collections.Generic;
using System.Data.Entity.Core.EntityClient;
using System.Threading.Tasks;
using SimpleModel;
using Xunit;
public class AsyncScenarios : FunctionalTestBase
{
#region Infrastructure/setup
public AsyncScenarios()
{
CreateMetadataFilesForSimpleModel();
}
#endregion
[Fact]
public void Async_query_returns_same_as_as_sync()
{
var query = @"
SELECT VALUE p
FROM CodeFirstContainer.Products AS p
WHERE p.ID < 5
ORDER BY p.ID";
var listSync = Query(SimpleModelEntityConnectionString, query);
var listAsync = QueryAsync(SimpleModelEntityConnectionString, query).Result;
Assert.Equal(listSync, listAsync, new ProductEqualityComparer());
}
[Fact]
public void Async_scalar_query_returns_same_as_as_sync()
{
var query = @"
COUNT(
SELECT VALUE p.ID
FROM CodeFirstContainer.Products AS p
WHERE p.ID < 5)";
var scalarSync = QueryScalar(SimpleModelEntityConnectionString, query);
var scalarAsync = QueryScalarAsync(SimpleModelEntityConnectionString, query).Result;
Assert.Equal(scalarSync, scalarAsync);
}
[Fact]
public void Async_nonquery_returns_same_as_as_sync()
{
var query = @"SELECT VALUE p FROM CodeFirstContainer.Products AS p";
var listSync = NonQuery(SimpleModelEntityConnectionString, query);
var listAsync = NonQueryAsync(SimpleModelEntityConnectionString, query).Result;
Assert.Equal(listSync, listAsync);
}
private async Task<List<Product>> QueryAsync(string connectionString, string commandString)
{
var connection = new EntityConnection(connectionString);
await connection.OpenAsync();
var command = new EntityCommand(commandString, connection);
var reader = command.ExecuteReaderAsync(CommandBehavior.SequentialAccess).Result;
var list = new List<Product>();
do
{
while (await reader.ReadAsync())
{
var values = new List<object>();
for (var i = 0; i < reader.FieldCount; i++)
{
values.Add(await reader.GetFieldValueAsync<object>(i));
}
list.Add(CreateProduct(values));
}
}
while (await reader.NextResultAsync());
connection.Close();
return list;
}
private List<Product> Query(string connectionString, string commandString)
{
var connection = new EntityConnection(connectionString);
connection.Open();
var command = new EntityCommand(commandString, connection);
var reader = command.ExecuteReader(CommandBehavior.SequentialAccess);
var list = new List<Product>();
do
{
while (reader.Read())
{
var values = new List<object>();
for (var i = 0; i < reader.FieldCount; i++)
{
values.Add(reader.GetFieldValue<object>(i));
}
list.Add(CreateProduct(values));
}
}
while (reader.NextResult());
connection.Close();
return list;
}
private Product CreateProduct(List<object> values)
{
var product = new Product();
product.Id = (int)values[0];
product.CategoryId = (string)values[1];
product.Name = (string)values[2];
return product;
}
private async Task<object> QueryScalarAsync(string connectionString, string commandString)
{
var connection = new EntityConnection(connectionString);
await connection.OpenAsync();
var command = new EntityCommand(commandString, connection);
var result = await command.ExecuteScalarAsync();
connection.Close();
return result;
}
private object QueryScalar(string connectionString, string commandString)
{
var connection = new EntityConnection(connectionString);
connection.Open();
var command = new EntityCommand(commandString, connection);
var result = command.ExecuteScalar();
connection.Close();
return result;
}
private async Task<object> NonQueryAsync(string connectionString, string commandString)
{
var connection = new EntityConnection(connectionString);
await connection.OpenAsync();
var command = new EntityCommand(commandString, connection);
var result = await command.ExecuteNonQueryAsync();
connection.Close();
return result;
}
private object NonQuery(string connectionString, string commandString)
{
var connection = new EntityConnection(connectionString);
connection.Open();
var command = new EntityCommand(commandString, connection);
var result = command.ExecuteNonQuery();
connection.Close();
return result;
}
private class ProductEqualityComparer : IEqualityComparer<Product>
{
public bool Equals(Product x, Product y)
{
if (x == null)
{
return y == null;
}
if (y == null)
{
return false;
}
return x.Id == y.Id &&
x.Name == y.Name &&
x.CategoryId == y.CategoryId;
}
public int GetHashCode(Product obj)
{
throw new NotImplementedException();
}
}
}
}
#endif
|