This topic demonstrates creating a method in Entity Developer NHibernate model, which will execute a parameterized query returning the table records that satisfy the query condition as the list of corresponding entity objects.
For this, perform the following steps:
1. | Add an entity for a table to the model. |
2. | In the Model Explorer context menu select the Add submenu and then select New Method, or click the corresponding button on the Model Explorer toolbar or on the main application toolbar. |
3. | In the Method Editor dialog specify the SQL for the method, set the entity as the method return type and customize the parameters: |
4. | Customize the parameters: |
5. | Click OK. |
The method is created.
As a result of the code generation for the model, the corresponding method is generated:
C#:
public static IList<testmodellermodel.department> GetDepartmentsByLocation(NHibernate.ISession session, string Loc)
{
NHibernate.IQuery query = session.GetNamedQuery(@"GetDepartmentsByLocation");
query.SetParameter(@"Loc", Loc);
return query.List<testmodellermodel.department>();
}
Visual Basic:
Public Shared Function GetDepartmentsByLocation ( _
session As NHibernate.ISession, Loc As String) As List(Of testmodellerModel.Department)
Dim query As NHibernate.IQuery = session.GetNamedQuery("GetDepartmentsByLocation")
query.SetParameter("Loc", Loc)
Return query.List(Of testmodellerModel.Department)()
End Function
Now it is possible to use this query in the application with the help of method wrapper. This allows working with the query with all possible convenience, as wrapper methods are strongly typed, are found by IntelliSense and have the corresponding signature.