NhiberNate Map by code 不包含 'id' 的定义
NhiberNate Map by code does not contain a definision for 'id'
我玩过 NHiberNate,我让它与 xml 定义一起工作,但我被困在我的 Map by Code 测试中。
我有以下代码:
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;
using NHibernate.Mapping;
namespace NHibernatePets
{
public class Pet
{
virtual public int id { get; set; }
virtual public string PetName { get; set; }
virtual public string Species { get; set; }
virtual public DateTime Birthday { get; set; }
virtual public string Speak()
{
return "Hi. My name is '" + PetName + "' and I'm a " + Species + " born on " + Birthday + ".";
}
}
public class PetMap : ClassMapping<Pet>
{
public PetMap()
{
table("Pet");
Lazy(true);
Id(x => x.Id, map => map.Generator(Generators.Identity));
Property(x => x.Petname, map => map.NotNullable(true));
Property(x => x.Species, map => map.NotNullable(true));
Property(x => x.Birthday, map => map.NotNullable(true));
}
当我按 f5 时,出现以下错误消息
Error 15 'NHibernatePets.Pet' does not contain a definition for 'Id' and no extension method 'Id' accepting a first argument of type 'NHibernatePets.Pet' could be found (are you missing a using directive or an assembly reference?)
}
我的计划是能够像这样对 PET 数据库进行查询
using (ISession session = OpenSession())
{
IQuery query = session.CreateQuery(" FROM Pet");
IList<Pet> pets = query.List<Pet>();
Console.Out.WriteLine("pets.Count = " + pets.Count);
pets.ToList().ForEach(p => Console.WriteLine(p.Speak()));
}
当我使用 xml 映射 table 时它起作用了。
我在这里做错了什么?
C# 区分大小写,因为 ID 属性 被命名为“id
”
public class Pet
{
virtual public int id { get; set; }
我们无法将其映射为 Id
Id(x => x.Id... // there is no Id on x === Pet
因此,要遵循 C# 中的通用命名约定,只需将 属性 名称更改为 Id
public class Pet
{
//virtual public int id { get; set; }
virtual public int Id { get; set; }
我玩过 NHiberNate,我让它与 xml 定义一起工作,但我被困在我的 Map by Code 测试中。 我有以下代码:
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;
using NHibernate.Mapping;
namespace NHibernatePets
{
public class Pet
{
virtual public int id { get; set; }
virtual public string PetName { get; set; }
virtual public string Species { get; set; }
virtual public DateTime Birthday { get; set; }
virtual public string Speak()
{
return "Hi. My name is '" + PetName + "' and I'm a " + Species + " born on " + Birthday + ".";
}
}
public class PetMap : ClassMapping<Pet>
{
public PetMap()
{
table("Pet");
Lazy(true);
Id(x => x.Id, map => map.Generator(Generators.Identity));
Property(x => x.Petname, map => map.NotNullable(true));
Property(x => x.Species, map => map.NotNullable(true));
Property(x => x.Birthday, map => map.NotNullable(true));
}
当我按 f5 时,出现以下错误消息
Error 15 'NHibernatePets.Pet' does not contain a definition for 'Id' and no extension method 'Id' accepting a first argument of type 'NHibernatePets.Pet' could be found (are you missing a using directive or an assembly reference?) }
我的计划是能够像这样对 PET 数据库进行查询
using (ISession session = OpenSession())
{
IQuery query = session.CreateQuery(" FROM Pet");
IList<Pet> pets = query.List<Pet>();
Console.Out.WriteLine("pets.Count = " + pets.Count);
pets.ToList().ForEach(p => Console.WriteLine(p.Speak()));
}
当我使用 xml 映射 table 时它起作用了。 我在这里做错了什么?
C# 区分大小写,因为 ID 属性 被命名为“id
”
public class Pet
{
virtual public int id { get; set; }
我们无法将其映射为 Id
Id(x => x.Id... // there is no Id on x === Pet
因此,要遵循 C# 中的通用命名约定,只需将 属性 名称更改为 Id
public class Pet
{
//virtual public int id { get; set; }
virtual public int Id { get; set; }