我怎么能强制Nhibernate将字符串替换为nvarchar而不是使用条件等于varchar?
我有这段代码
var permissionCriteria = DetachedCriteria.For<Permission>("p")
.SetProjection(Projections.Id())
.Add(Restrictions.Eq("UserId", user.UserId))
.Add(Expression.EqProperty("p.TypeId", "T.Id"));
在以下sql中翻译
and exists (SELECT this_0_.Id as y0_
FROM Permissions this_0_
WHERE this_0_.UserId = 'USERNAME'/* @p5 */
and this_0_.TypeId = t2_.Id);
问题是“USERNAME”之前缺少“N”导致sql server进行扫描而不是搜索。
属性UserId由NH更正地映射到nvarchar,但它仍然用作ANSI字符串
修改 这是班级
public class Permission : EntityBase
{
public virtual string Id { get; set; }
public virtual string UserId { get; set; }
....
}
和映射
public class PermissionOverrides : IAutoMappingOverride<Permission>
{
public void Override(AutoMapping<Permission> mapping)
{
mapping.Table("Permissions");
mapping.Id(x => x.Id).GeneratedBy.Assigned();
mapping.Map(x => x.UserId);
....
}
}
我也试过
mapping.Map(x => x.UserId).CustomSqlType("nvarchar(20)");
没有运气