NHibernate问题具有分配的字符串ID和不同的大小写字符

| 在NHibernate上保存带有分配的字符串ID的实体时,我遇到问题...我尝试用一​​个示例来解释问题。 好吧,假设我执行此语句,则在数据库上具有ID为“ AAA”的实体
ENTITYTYPE entity = Session.Get<ENTITYTYPE>(\"AAA\");
ENTITYTYPE newentity = new ENTITYTYPE() { Id = \"aaa\" };

Session.Delete(entity);
Session.Save(newentity);
Session.Flush();
在刷新NHibernate上,出现以下消息异常:\“无法将数据库状态与会话\” / \“违反PRIMARY KEY \” 区分大小写的ID似乎有问题,如果我在\“ newentity \”的ID上使用\“ AAA \”,那么它可以工作,但是在我的情况下并不是那么容易,我必须找到一个替代解决方案。 如何避免这种异常?你能帮助我吗?     
已邀请:
        我不知道它是否足够,但是您可以通过以下方式控制它:
public override bool Equals(object obj)
{
T other = obj as T;
if (other == null)
    return false;

// handle the case of comparing two NEW objects
bool otherIsTransient = Equals(other.Id, Guid.Empty);
bool thisIsTransient = Equals(Id, Guid.Empty);
if (otherIsTransient && thisIsTransient)
    return ReferenceEquals(other, this);

return other.Id.ToUpper().Equals(Id.ToUpper());
}
在比较实体时使用ToUpper()或ToLower()方法,也可以使用String.Compare(stringA,strngB,StringComparison.OrdinalIgnoreCase)。 如果您希望获得更多控制权,并且这是您的目标,则可以按照以下说明创建自定义ID生成器: http://nhibernate.info/doc/howto/various/creating-a-custom-id-generator-for-nhibernate.html 更新 您是否尝试过创建自定义GetIgnoreCase(...)? 我认为也可以通过加载器标签覆盖实体映射文件中默认由Get方法生成的SELECT语句,如下例所示:
       ...
      <loader query-ref=\"loadProducts\"/>
 </class>

 <sql-query name=\"loadProducts\">
 <return alias=\"prod\" class=\"Product\" />
 <![CDATA[
  select 
    ProductID as {prod.ProductID}, 
    UnitPrice as {prod.UnitPrice}, 
    ProductName as {pod.ProductName}
  from Products prod
  order by ProductID desc
]]>
您可以尝试修改返回大写ID的select语句。 更新 经过深入调查,我认为可以使用拦截器来解决您的问题! 在这里阅读: http://knol.google.com/k/fabio-maulo/nhibernate-chapter-11-interceptors-and/1nr4enxv3dpeq/14# 此处有更多文档: http://blog.scooletz.com/2011/02/22/nhibernate-interceptor-magic-tricks-pt-5/ 像这样:
 public class TestInterceptor
  : EmptyInterceptor, IInterceptor
{
    private readonly IInterceptor innerInterceptor;

    public TestInterceptor(IInterceptor innerInterceptor)
    {
        this.innerInterceptor = this.innerInterceptor ?? new EmptyInterceptor();
    }

    public override object GetEntity(string entityName, object id)
    {
        if (id is string)
            id = id.ToString().ToUpper();

        return this.innerInterceptor.GetEntity(entityName, id);

    }

}
并像这样流利地注册它:
return Fluently.Configure()
           ...
            .ExposeConfiguration(c =>{c.Interceptor = new TestInterceptor(c.Interceptor ?? new EmptyInterceptor());})
            ...
            .BuildConfiguration();
    

要回复问题请先登录注册