NHibernate: Now that Session.Find() is obsolete, how do I replace this?
I see that Session.Find(string query, object[] values, IType[] types) is obsolete, and the suggestion is to use CreateQuery().SetParameterList().List() instead.
So if I already have code that looks like this:
var query = "from TABLE t where t.COL1 = ? and t.COL2 = ?";
var vals = new[] { qc1, qc2 };
var types = new[] { ScalarType.String, ScalarType.String };
Session.Find(query, vals, t开发者_如何学编程ypes);
What would I pass to SetParameterList's name argument?
What you're looking for is probably something like this:
session.CreateQuery("from Entity t where t.COL1 = :col1 and t.COL2 = :col2")
.SetString("col1", qc1)
.SetString("col1", qc2)
.List<Entity>();
.SetParameterList(...)
takes an ICollection
as argument, and can be used e.g. with the in
clause:
session.CreateQuery("from Entity t where t.COL1 in (:cols)")
.SetParameterList("cols", new [] { "someValue", "anotherValue", "etc"})
.List<Entity>();
I think I have to do this:
var q = Session.CreateQuery(query);
for (int i = 0; i < vals.Length; i++)
{
q.SetParameter(i, vals[i], types[i]);
}
精彩评论