C# NhiberNate映射按代码不包含对';id';
我和NHiberNate打过交道,我让它与xml定义一起工作,但我被代码测试困在地图中。 我有以下代码:C# NhiberNate映射按代码不包含对';id';,c#,nhibernate,nhibernate-mapping,mapping-by-code,C#,Nhibernate,Nhibernate Mapping,Mapping By Code,我和NHiberNate打过交道,我让它与xml定义一起工作,但我被代码测试困在地图中。 我有以下代码: 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.
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));
}
使用系统;
使用System.Collections.Generic;
运用系统反思;
使用System.Linq;
使用NHibernate;
使用NHibernate.Cfg;
使用NHibernate.Mapping.ByCode;
使用NHibernate.Mapping.ByCode.Conformist;
使用NHibernate.Mapping;
名称空间NHibernatePets
{
公营宠物
{
虚拟公共int id{get;set;}
虚拟公共字符串PetName{get;set;}
虚拟公共字符串种类{get;set;}
虚拟公共日期时间生日{get;set;}
虚拟公共字符串Speak()
{
返回“嗨,我的名字是“+”宠物名“+”,我是“+物种+”出生于“+生日+”;
}
}
公共类PetMap:类映射
{
公共地图()
{
表(“Pet”);
懒惰(真);
Id(x=>x.Id,map=>map.Generator(Generators.Identity));
属性(x=>x.Petname,map=>map.NotNullable(true));
属性(x=>x.Species,map=>map.NotNullable(true));
属性(x=>x.birth,map=>map.NotNullable(true));
}
当我按f5时,我给出以下错误消息
错误15“NHibernatePets.Pet”不包含“Id”的定义,并且找不到接受“NHibernatePets.Pet”类型的第一个参数的扩展方法“Id”(是否缺少using指令或程序集引用?)
}
我的计划是能够像这样对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()));
}
使用(ISession session=OpenSession())
{
IQuery=session.CreateQuery(“来自Pet”);
IList=query.List();
Console.Out.WriteLine(“pets.Count=“+pets.Count”);
pets.ToList().ForEach(p=>Console.WriteLine(p.Speak());
}
当我使用xml映射表时,它起了作用。
我在这里做错了什么?C#区分大小写,因为ID属性名为“ID
”
我们无法将其映射为Id
Id(x => x.Id... // there is no Id on x === Pet
因此,要遵循C#中常见的命名约定,只需将属性名称更改为Id
public class Pet
{
virtual public int id { get; set; }
public class Pet
{
//virtual public int id { get; set; }
virtual public int Id { get; set; }
C#区分大小写,因为ID属性名为“ID
”
我们无法将其映射为Id
Id(x => x.Id... // there is no Id on x === Pet
因此,要遵循C#中常见的命名约定,只需将属性名称更改为Id
public class Pet
{
virtual public int id { get; set; }
public class Pet
{
//virtual public int id { get; set; }
virtual public int Id { get; set; }
享受伟大的恩希伯内特,先生;)享受伟大的恩希伯内特,先生;)