如何:执行参数化查询(实体框架)
本主题说明如何使用 ObjectQuery 执行具有参数的 Entity SQL 查询。本示例将两个参数传递到 ObjectQuery,执行查询,并循环访问 Contact
项的集合。将使用以下每种 实体框架 查询技术演示同一示例:
Entity SQL with ObjectQuery<T>
ObjectQuery <T> 的查询生成器方法
本主题中的示例基于 AdventureWorks 销售模型 (EDM)。若要运行本示例中的代码,必须已将 AdventureWorks 销售模型添加到您的项目中,并将项目配置为使用 实体框架。为此,请完成如何:手动配置实体框架项目和如何:手动定义实体数据模型(实体框架) 中的过程。也可以使用实体数据模型向导定义 AdventureWorks 销售模型。有关更多信息,请参见如何:使用实体数据模型向导(实体框架)。
示例
以下是 Entity SQL 示例。
Using advWorksContext As New AdventureWorksEntities
Try
' Create a query that takes two parameters.
Dim queryString As String = "SELECT VALUE Contact FROM AdventureWorksEntities.Contact " & _
"AS Contact WHERE Contact.LastName = @ln AND " & _
"Contact.FirstName = @fn"
' Add parameters to the collection.
Dim contactQuery As New ObjectQuery(Of Contact)(queryString, advWorksContext, MergeOption.NoTracking)
contactQuery.Parameters.Add(New ObjectParameter("ln", "Adams"))
contactQuery.Parameters.Add(New ObjectParameter("fn", "Frances"))
' Iterate through the collection of Contact items.
For Each result As Contact In contactQuery
Console.WriteLine("Last Name: {0} First Name: {1}", _
result.LastName, result.FirstName)
Next
Catch ex As EntityException
Console.WriteLine(ex.ToString)
Catch ex As InvalidOperationException
Console.WriteLine(ex.ToString())
End Try
End Using
using (AdventureWorksEntities advWorksContext =
new AdventureWorksEntities())
{
try
{
// Create a query that takes two parameters.
string queryString =
@"SELECT VALUE Contact FROM AdventureWorksEntities.Contact
AS Contact WHERE Contact.LastName = @ln AND
Contact.FirstName = @fn";
ObjectQuery<Contact> contactQuery =
new ObjectQuery<Contact>(queryString, advWorksContext);
// Add parameters to the collection.
contactQuery.Parameters.Add(new ObjectParameter("ln", "Adams"));
contactQuery.Parameters.Add(new ObjectParameter("fn", "Frances"));
// Iterate through the collection of Contact items.
foreach (Contact result in contactQuery)
Console.WriteLine("Last Name: {0}; First Name: {1}",
result.LastName, result.FirstName);
}
catch (EntityException ex)
{
Console.WriteLine(ex.ToString());
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.ToString());
}
}
这是查询生成器方法示例。
Dim firstName As String = "Frances"
Dim lastName As String = "Adams"
Using context As New AdventureWorksEntities
Try
' Get the contacts with the specified name.
Dim contactQuery As ObjectQuery(Of Contact) = _
context.Contact _
.Where("it.LastName = @ln AND it.FirstName = @fn", _
New ObjectParameter("ln", lastName), _
New ObjectParameter("fn", firstName))
' Iterate through the collection of Contact items.
For Each result As Contact In contactQuery
Console.WriteLine("Last Name:{0}First Name: {1}", _
result.LastName, result.FirstName)
Next
Catch ex As EntitySqlException
Console.WriteLine(ex.ToString())
End Try
End Using
string firstName = @"Frances";
string lastName = @"Adams";
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
try
{
// Get the contacts with the specified name.
ObjectQuery<Contact> contactQuery = context.Contact
.Where("it.LastName = @ln AND it.FirstName = @fn",
new ObjectParameter("ln", lastName),
new ObjectParameter("fn", firstName));
// Iterate through the collection of Contact items.
foreach (Contact result in contactQuery)
Console.WriteLine("Last Name: {0}; First Name: {1}",
result.LastName, result.FirstName);
}
catch (EntitySqlException ex)
{
Console.WriteLine(ex.ToString());
}
}
另请参见
任务
如何:执行返回实体类型的查询(实体框架)
如何:执行返回匿名类型的查询(实体框架)
如何:执行返回基元类型的查询(实体框架)