nHibernate在生成的SQL中使用了错误的别名

时间:2014-04-07 17:28:18

标签: sql c#-4.0 nhibernate fluent-nhibernate nhibernate-mapping

当我尝试使用以下命令查询时,nHibernate会抛出ADOException:

session.Query<Owner>()
    .Where(x => x.Animal.Environments.Any(e => e == "Garage")
        && x.Animal.Name == "Lassie" && x.OwnerName == "John Doe");

生成的SQL表明它使用的是不存在的别名。

这里是映射和实体:

    public class Owner
    {
        public Owner()
        {

        }

        public Owner(IAnimalBase animal)
        {
            this.Animal = animal;
        }

        public virtual IAnimalBase Animal { get; protected set; }

        public virtual string OwnerName { get; set; }

        public virtual Guid Id { get; set; }
    }

    public class Dog : AnimalBase
    {
        public virtual string BarkType { get; set; }
    }

    public interface IAnimalBase
    {
        string Name { get; set; }

        IList<string> Environments { get; set; }
        Guid Id { get; set; }
    }

    public abstract class AnimalBase : IAnimalBase
    {
        public virtual Guid Id { get; set; }

        public virtual string Name { get; set; }

        public virtual IList<string> Environments { get; set; }
    }

public class DogMap : SubclassMap<Dog>
{
    public DogMap()
    {
        this.Map(x => x.BarkType);

        this.DiscriminatorValue("D");
    }
}

    public OwnerMap()
    {
        this.Id(x => x.Id).GeneratedBy.GuidComb();

        this.Map(x => x.OwnerName);

        this.References(x => x.Animal).Class<AnimalBase>().Not.LazyLoad().Cascade.All();
    }

    public AnimalBaseMap()
    {
        this.Id(x => x.Id).GeneratedBy.GuidComb();

        this.HasMany(x => x.Environments).Table("AnimalEnvironments").Element("Environment").Cascade.All();

        this.Map(x => x.Name);

        this.DiscriminateSubClassesOnColumn("Aid");
    }

抱怨animalbase1_.Id。

select owner0_.Id        as Id2_,
       owner0_.OwnerName as OwnerName2_,
       owner0_.Animal_id as Animal3_2_
from   "Owner" owner0_,
       "AnimalBase" animalbase3_
where  owner0_.Animal_id = animalbase3_.Id
       and (exists (select environmen2_.Environment
                    from   AnimalEnvironments environmen2_
                    where  owner0_.Animal_id = animalbase1_.Id
                           and animalbase1_.Id = environmen2_.AnimalBase_id
                           and environmen2_.Environment = 'Garage' /* @p0 */))
       and animalbase3_.Name = 'Lassie' /* @p1 */
       and owner0_.OwnerName = 'John Doe' /* @p2 */

谢谢!

1 个答案:

答案 0 :(得分:0)

您可能希望映射到实际的类而不是接口。我确信有充分的理由使用我从未有过的界面......

参考:Fluent NHibernate, working with interfaces

未经测试只是一个基本示例

public class Owner
 {
    public Owner()
     {
         Dog = new List<Dog>();
     }
     public virtual IList<Dog> Dog { get; set; }
     public virtual string OwnerName { get; set; }
     public virtual Guid Id { get; set; }
 }

 public class Dog 
 {
     public Dog()
     {
         AnimalBase = new AnimalBase();
     }
     public virtual string BarkType { get; set; }
     public virtual AnimalBase AnimalBase { get; set; }
 }

 public class AnimalBase 
 {
     public AnimalBase()
     {
         Environments = new List<strings>();
     }
     public virtual string Name { get; set; }
     public virtual IList<string> Environments { get; set; }
 }