代码错误消息的 NHibernate 映射无法执行查询
NHibernate mapping by code error message could not execute query
我是 c# 和 NHibernate 的新手,所以如果这个问题不合时宜,请原谅我。
我正在通过代码在 Nhibernate 中映射 table,我不断收到此错误:
could not execute query
我创建了以下 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));
}
}
我运行下面的查询
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();
}
}
它以异常结束并显示以下消息
无法执行查询:
[ 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 ]
在我看来,NhiberNate 想要一个 Id 列,即使 table 中有 none。
所以我确实尝试通过将其添加到 class PoliceData
在代码中创建一个 Id
virtual 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 是否需要
在映射 class 中定义的列
map.Generator(Generators.Identity));
它有什么作用?
任何映射的实体都必须映射 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));
同时检查
Mapping-by-Code - Id, NaturalId
片段如何映射 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);
});
我是 c# 和 NHibernate 的新手,所以如果这个问题不合时宜,请原谅我。
我正在通过代码在 Nhibernate 中映射 table,我不断收到此错误:
could not execute query
我创建了以下 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));
}
}
我运行下面的查询
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();
}
}
它以异常结束并显示以下消息
无法执行查询:
[ 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 ]
在我看来,NhiberNate 想要一个 Id 列,即使 table 中有 none。
所以我确实尝试通过将其添加到 class 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 是否需要
在映射 class 中定义的列map.Generator(Generators.Identity));
它有什么作用?
任何映射的实体都必须映射 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));
同时检查
Mapping-by-Code - Id, NaturalId
片段如何映射 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);
});