Search code examples
c#nhibernatefluent-nhibernatenhibernate-mappingnhibernate-mapping-by-code

NHibernate mapping by code error message could not execute query


I am new to c# and NHibernate so please forgive me if this question is out of line.

I am working on mapping a table in Nhibernate by code i keep getting this error:

could not execute query

I have creates following classes

class PoliceData
{     
    virtual public int policyNumber { get; set; }
    virtual public String product { get; set; }
    virtual public String Navn { get; set; }
    virtual public String Adresse { get; set; }
    virtual public String Husnr { get; set; }
    virtual public String Postnr { get; set; }
    virtual public String By { get; set; }
    virtual public String Lattitude { get; set; }
    virtual public String Longitude { get; set; }
    virtual public String Cell100M { get; set; }
    virtual public String Cell1KM { get; set; }
    virtual public String Cell10KM { get; set; }
}    
class PoliceDataMap   : ClassMapping<PoliceData>
{
    public PoliceDataMap()
    {
        Table("policeDataView");
        Lazy(true);
        Property(x => x.policyNumber, map => map.NotNullable(true));
        Property(x => x.product, map => map.NotNullable(true));
        Property(x => x.Navn, map => map.NotNullable(true));
        Property(x => x.Adresse, map => map.NotNullable(true));
        Property(x => x.Husnr, map => map.NotNullable(true));
        Property(x => x.Postnr, map => map.NotNullable(true));
        Property(x => x.By, map => map.NotNullable(true));
        Property(x => x.Lattitude, map => map.NotNullable(true));
        Property(x => x.Longitude, map => map.NotNullable(true));
        Property(x => x.Cell100M, map => map.NotNullable(true));
        Property(x => x.Cell1KM, map => map.NotNullable(true));
        Property(x => x.Cell10KM, map => map.NotNullable(true));
    }        
}

I'm running the following query

public DbFactory()
{
    using (ISession session = OpenSession())
    {
        IList<PoliceData> policedata = session.Query<PoliceData>().Where(p => p.policyNumber == 053126703).ToList();
        //IList<Pet> pets = query.List<Pet>();
        // Console.Out.WriteLine("pets.Count = " + pets.Count);
        // pets.ToList().ForEach(p => Console.WriteLine(p.PetName));
        // Console.Read();
    }
}

It ends in exception with the following message

could not execute query:

[ select policedata0_.id as id0_, policedata0_.policyNumber as policyNu2_0_, policedata0_.product as product0_, policedata0_.Navn as Navn0_, policedata0_.Adresse as Adresse0_, policedata0_.Husnr as Husnr0_, policedata0_.Postnr as Postnr0_, policedata0_.Bynavn as Bynavn0_, policedata0_.Lattitude as Lattitude0_, policedata0_.Longitude as Longitude0_, policedata0_.Cell100M as Cell11_0_, policedata0_.Cell1KM as Cell12_0_, policedata0_.Cell10KM as Cell13_0_ from policeDataView policedata0_ where policedata0_.policyNumber=@p0 ]

It seems to me that NhiberNate want a Id column even though there are none in the table.

So i did try to create a Id in the code by adding this to class PoliceData

virtual public int Id { get; set; }

and adding this to PoliceDataMap

Id(x => x.id, map => map.Generator(Generators.Identity));

Now im getting compile error:

the name 'map' does not exits in the current context 

What could I do to solve this, Does NHibernate need a column defined in the map class by

map.Generator(Generators.Identity));

What does it do ?


Solution

  • Any mapped entity must have mapped ID, so you have to provide some. But in case that you have ID like this:

    virtual public int Id { get; set; }
    

    Mapping should be

    //Id(x => x.id, map => map.Generator(Generators.Identity));
    Id(x => x.Id, map => map.Generator(Generators.Identity));
    

    Also check the

    Mapping-by-Code - Id, NaturalId

    snippet how to mapp ID:

    Id(x => x.Id, m =>
    {
        m.Column("id");
    
        m.Generator(Generators.Native, g => g.Params(new
        {
            // generator-specific options
        }));
    
        m.Length(10);
        m.Type(new Int32Type());
        m.Access(Accessor.Field);
    });