我是c#和NHibernate的新手,所以如果这个问题不合适,请原谅我。
我正在通过代码在Nhibernate中映射表我不断收到此错误:
could not execute query
我创建了以下类
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));
}
}
我正在运行以下查询
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();
}
}
以异常方式结束,并显示以下消息
无法执行查询:
[选择policedata0_.id如id0_,policedata0_.policyNumber如policyNu2_0_,policedata0_.product如product0_,policedata0_.Navn如Navn0_,policedata0_.Adresse如Adresse0_,policedata0_.Husnr如Husnr0_,policedata0_.Postnr如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]
在我看来,NhiberNate想要一个Id列,即使表中没有。
所以我尝试通过将其添加到类PoliceData
来尝试在代码中创建Idvirtual public int Id { get; set; }
并将其添加到PoliceDataMap
Id(x => x.id, map => map.Generator(Generators.Identity));
现在我得到编译错误:
the name 'map' does not exits in the current context
我能做些什么来解决这个问题,NHibernate是否需要通过
在地图类中定义一个列map.Generator(Generators.Identity));
它做什么?
答案 0 :(得分:0)
任何映射的实体都必须映射 ID ,因此您必须提供一些。但如果您有 ID ,请执行以下操作:
virtual public int Id { get; set; }
映射应该是
//Id(x => x.id, map => map.Generator(Generators.Identity));
Id(x => x.Id, map => map.Generator(Generators.Identity));
同时检查
摘录如何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);
});