本文整理汇总了C#中EntityManager.ExecuteQuery方法的典型用法代码示例。如果您正苦于以下问题:C# EntityManager.ExecuteQuery方法的具体用法?C# EntityManager.ExecuteQuery怎么用?C# EntityManager.ExecuteQuery使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类EntityManager
的用法示例。
在下文中一共展示了EntityManager.ExecuteQuery方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ParallelQueries
public async Task ParallelQueries() {
var entityManager = new EntityManager(_serviceName);
await entityManager.FetchMetadata();
var idQuery = new EntityQuery<Customer>();
var entities = await entityManager.ExecuteQuery(idQuery);
var n = 20;
var ids = entities.Select(c => c.CustomerID).Take(n).ToArray();
// In case there aren't n customers available
var actualCustomers = ids.Count();
var numExecutions = 0;
var tasks = ids.Select(id =>
{
++numExecutions;
var query = new EntityQuery<Customer>().Where(c => c.CustomerID == ids[0]);
return entityManager.ExecuteQuery(query);
// Best practice is to apply ToList() or ToArray() to the task collection immediately
// Realizing the collection more than once causes multiple executions of the anon method
}).ToList();
var numTasks = tasks.Count();
// Result of WhenAll is an array of results from the individual anonymous method executions
// Each individual result is a collection of customers
// (only one in this case because of the condition on CustomerId)
var results = await Task.WhenAll(tasks);
var numCustomers = results.Sum(customers => customers.Count());
Assert.AreEqual(actualCustomers, numTasks, "Number of tasks should be " + actualCustomers + ", not " + numTasks);
Assert.AreEqual(actualCustomers, numExecutions, "Number of excutions should be " + actualCustomers + ", not " + numExecutions);
Assert.AreEqual(actualCustomers, numCustomers, actualCustomers + " customers should be returned, not " + numCustomers);
}
示例2: SimpleConcurrencyFault
public async Task SimpleConcurrencyFault() {
Configuration.Instance.ProbeAssemblies(typeof(Customer).Assembly);
// Query Alfred
var entityManager1 = new EntityManager(_northwindServiceName);
var query = new EntityQuery<Customer>().Where(c => c.CustomerID == _alfredsID);
var alfred1 = (await entityManager1.ExecuteQuery(query)).FirstOrDefault();
// Query a second copy of Alfred in a second entity manager
var entityManager2 = new EntityManager(_northwindServiceName);
var alfred2 = (await entityManager2.ExecuteQuery(query)).FirstOrDefault();
// Modify and save the first Alfred
alfred1.ContactTitle += "X";
// Currently, this throws due to "changes to an original record may not be saved"
// ...whatever that means
var saveResult1 = await entityManager1.SaveChanges();
// Attempt to modify and save the second Alfred
alfred2.ContactName += "Y";
try {
var saveResult2 = await entityManager2.SaveChanges();
}
catch (SaveException e) {
var message = e.Message;
}
}
示例3: QueryRoles
public async Task QueryRoles()
{
//Assert.Inconclusive("See comments in the QueryRoles test");
// Issues:
//
// 1. When the RoleType column was not present in the Role entity in the database (NorthwindIB.sdf),
// the query failed with "Internal Server Error". A more explanatory message would be nice.
// The RoleType column has been added (nullable int), so this error no longer occurs.
//
// 2. Comment out the RoleType property in the client model (Model.cs in Client\Model_Northwind.Sharp project lines 514-517).
// In this case, the client throws a null reference exception in CsdlMetadataProcessor.cs.
// This is the FIRST problem reported by the user. A more informative message would be helpful.
//
// Note that this condition causes many other tests to fail as well.
//
// 3. Uncomment the RoleType property in the client model. Then the client throws in JsonEntityConverter.cs.
// This is the SECOND problem reported by the user. This looks like a genuine bug that should be fixed.
//
var manager = new EntityManager(_serviceName);
// Metadata must be fetched before CreateEntity() can be called
await manager.FetchMetadata();
var query = new EntityQuery<Role>();
var allRoles = await manager.ExecuteQuery(query);
Assert.IsTrue(allRoles.Any(), "There should be some roles defined");
}
示例4: MetadataMissingClrPropertyQuery
public async Task MetadataMissingClrPropertyQuery() {
var em = new EntityManager(_serviceName);
em.MetadataStore.NamingConvention = new MorphedClassNamingConvention();
em.MetadataStore.AllowedMetadataMismatchTypes = MetadataMismatchTypes.AllAllowable;
var q = new EntityQuery<PartialFoo.Customer>().Where(c => c.CompanyName.StartsWith("B"));
var r0 = await em.ExecuteQuery(q);
Assert.IsTrue(r0.Count() > 0);
}
示例5: SimpleCall
public async Task SimpleCall() {
//return;
var em1 = new EntityManager(_dataService);
em1.MetadataStore.NamingConvention = new EdmundsNamingConvention();
Model.Edmunds.Config.Initialize(em1.MetadataStore);
var initParameters = InitialParameters();
var q = new EntityQuery<Make>().From("vehicle/makerepository/findall")
.WithParameters(initParameters).With(new EdmundsJsonResultsAdapter());
var r = await em1.ExecuteQuery(q);
Assert.IsTrue(r.Any());
}
示例6: QueryWithAFilter
public async Task QueryWithAFilter()
{
var manager = new EntityManager(_serviceName);
// Metadata must be fetched before CreateEntity() can be called
await manager.FetchMetadata();
// Ensure there is at least one active todo item named Wine in the database
var wines = await new EntityQuery<TodoItem>().Where(td => td.Description == "Wine" && !td.IsArchived && !td.IsDone).Execute(manager);
if (!wines.Any()) {
var newWine = manager.CreateEntity<TodoItem>(new {Description = "Wine"});
await manager.SaveChanges();
}
manager.Clear();
//Snippet1
var query1 = new EntityQuery<TodoItem>();
var allTodos = await manager.ExecuteQuery(query1);
Assert.IsTrue(allTodos.Any(), "No TodoItems in the database");
//Snippet2
var query2 = query1.Where(td => !td.IsArchived);
var unarchivedTodos = await manager.ExecuteQuery(query2);
Assert.IsTrue(unarchivedTodos.Any(), "No unarchived TodoItems in the database");
//Snippet3
var query3 = query1.Where(td => !td.IsArchived && !td.IsDone);
var activeTodos = await manager.ExecuteQuery(query3);
Assert.IsTrue(activeTodos.Any(), "No active TodoItems in the database");
//Snippet4
var query4 = query1.Where(td => td.Description.Contains("Wine"));
var wineTodos = await manager.ExecuteQuery(query4);
Assert.IsTrue(allTodos.Any(), "No Wine TodoItems in the database");
//Snippet5
// Execute above query locally
var localWineTodos = manager.ExecuteQueryLocally(query4);
Assert.AreEqual(wineTodos.Count(), localWineTodos.Count(), "Got " + localWineTodos.Count() + " local wine TodoItems");
}
示例7: ParallelQueriesBatched
public async Task ParallelQueriesBatched() {
var entityManager = new EntityManager(_serviceName);
await entityManager.FetchMetadata();
var idQuery = new EntityQuery<Customer>();
var entities = await entityManager.ExecuteQuery(idQuery);
var n = 20;
var ids = entities.Select(c => c.CustomerID).Take(n).ToArray();
// In case there aren't n customers available
var actualCustomers = ids.Count();
int allowedParallelQueries = 5;
int numActiveQueries = 0;
int maxActiveQueries = 0;
var semaphore = new SemaphoreSlim(allowedParallelQueries, allowedParallelQueries);
var tasks = ids.Select(async id =>
{
await semaphore.WaitAsync();
var query = new EntityQuery<Customer>().Where(c => c.CustomerID == ids[0]);
// Place the release of the semaphore in a finally block to ensure it always gets released
try {
maxActiveQueries = Math.Max(maxActiveQueries, ++numActiveQueries);
return await entityManager.ExecuteQuery(query);
}
catch (Exception e) {
Assert.Fail("ExecuteQuery() threw " + e.GetType().Name + ": " + e.Message);
return new Customer[0];
}
finally {
--numActiveQueries;
semaphore.Release();
}
// Best practice is to apply ToList() to the task collection immediately
// Realizing the collection more than once causes multiple executions of the anon method
}).ToList();
// Result of WhenAll is an array of results from the individual anonymous method executions
// Each individual result is a collection of customers
// (only one in this case because of the condition on CustomerId)
var results = await Task.WhenAll(tasks);
var numQueriedCustomers = results.Sum(customers => customers.Count());
Assert.IsTrue(maxActiveQueries <= allowedParallelQueries, "Number of active queries should not exceed " + allowedParallelQueries + ". The max was " + maxActiveQueries);
Assert.AreEqual(actualCustomers, numQueriedCustomers, actualCustomers + " customers should be returned, not " + numQueriedCustomers);
}
示例8: CaptureAddlSavesDoneOnServer
public async Task CaptureAddlSavesDoneOnServer() {
var em1 = await TestFns.NewEm(_serviceName);
// ok(true, "Skipped test - OData does not support server interception or alt resources");
// ok(true, "N/A for Mongo - test not yet implemented - requires server side async call");
var q = new EntityQuery<Category>().Where(c => c.CategoryName.StartsWith("Beverage")).Expand("Products");
var r = await q.Execute(em1);
var category = r.First();
var products = category.Products;
var prices = products.Select(p => p.UnitPrice).ToList();
category.CategoryName = TestFns.MorphString(category.CategoryName);
var so = new SaveOptions(tag: "increaseProductPrice");
var sr = await em1.SaveChanges(null, so);
Assert.IsTrue(sr.Entities.Count == 13, "should have saved 13 entities + 1 category + 12 products");
Assert.IsTrue(sr.Entities.OfType<Product>().Count() == 12, "should be 12 products");
var ek = category.EntityAspect.EntityKey;
var em2 = new EntityManager(em1);
var r2 = await em2.ExecuteQuery(ek.ToQuery<Category>().Expand("Products"));
var cat2 = r2.First();
var products2 = cat2.Products;
var newPrices = products2.Select(p => p.UnitPrice).ToList();
Assert.IsTrue(!prices.SequenceEqual(newPrices), "prices should have changed");
}
示例9: UpdateProduct
private async Task UpdateProduct(EntityManager em, int productID) {
// ok(true, "TODO for Mongo - needs to be written specifically for Mongo - should succeed in Mongo");
var q0 = new EntityQuery<Product>().Where(p => p.ProductID == productID);
var r0 = await em.ExecuteQuery(q0);
Assert.IsTrue(r0.Count() == 1, "should be 1 result");
var prod = r0.First();
prod.UnitsInStock = (short)(prod.UnitsInStock.HasValue ? prod.UnitsInStock.Value + 1 : 1);
var sr0 = await em.SaveChanges();
Assert.IsTrue(sr0.Entities.Count == 1);
}
示例10: AllCustomers_Exceptions
public async Task AllCustomers_Exceptions() {
var entityManager = new EntityManager(_serviceName);
// All instances of Customer
var query = new EntityQuery<Customer>();
// Capture result using try-catch
try {
var results = await entityManager.ExecuteQuery(query);
var count = results.Count();
Assert.IsTrue(count > 0, "Customer query returned " + count + " customers");
} catch (Exception e) {
var message = TestFns.FormatException(e);
Assert.Fail(message);
}
}
示例11: AllCustomers_Task
public async Task AllCustomers_Task() {
var entityManager = new EntityManager(_serviceName);
// All instances of Customer
var query = new EntityQuery<Customer>(); // Alternate way to create a basic EntityQuery
// Handle async Task results explicitly
await entityManager.ExecuteQuery(query).ContinueWith(task => {
if (task.IsFaulted) {
var message = TestFns.FormatException(task.Exception);
Assert.Fail(message);
} else {
var count = task.Result.Count();
Assert.IsTrue(count > 0, "Customer query returned " + count + " customers");
}
});
}
示例12: QueryEntityTwice
public async Task QueryEntityTwice() {
var entityManager = new EntityManager(_serviceName);
await entityManager.FetchMetadata();
// Query entity twice
var query1 = new EntityQuery<Customer>().Where(c => c.CustomerID == _alfredsID);
var alfred1 = (await entityManager.ExecuteQuery(query1)).FirstOrDefault();
Assert.IsNotNull(alfred1, "Alfred should be found by Id");
var query2 = new EntityQuery<Customer>().Where(c => c.CompanyName == alfred1.CompanyName);
var alfred2 = (await entityManager.ExecuteQuery(query2)).FirstOrDefault();
Assert.IsTrue(ReferenceEquals(alfred1, alfred2), "Successive queries should return same entity");
}