NHibernate.MappingException: Unknown entity class: QuickStart.Cat

zbluestar 2007-06-12 10:23:35
异常详细信息: NHibernate.MappingException: Unknown entity class: QuickStart.Cat

源错误:


行 28: princess.Weight = 7.4f;
行 29:
行 30: session.Save(princess);
行 31: tx.Commit();

文件: c:\inetpub\wwwroot\quickstart\webform1.aspx.cs 行: 30


cat.hbm.xml文件:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="QuickStart" assembly="QuickStart">
<class name="Cat" table="Cat">
<!-- A 32 hex character is our surrogate key. It's automatically
generated by NHibernate with the UUID pattern. -->
<id name="Id">
<column name="CatId" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex" />
</id>
<!-- A cat has to have a name, but it shouldn' be too long. -->
<property name="Name">
<column name="Name" length="16" not-null="true" />
</property>
<property name="Sex" />
<property name="Weight"/>
</class>
</hibernate-mapping>

web.config文件

<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
</configSections>
<!-- Add this element -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.connection_string">Server=(local);database=NHibernate;user id=sa;password=sa</property>
<property name="hibernate.connection.driver_class">NHibernate.Driver.SqlClientDriver</property>

<mapping assembly="QuickStart" />
</session-factory>
</hibernate-configuration>

cat类:
public class Cat
{
private string id;
private string name;
private char sex;
private float weight;

public Cat()
{

}

public string Id
{
get { return id; }
set { id = value; }
}

public string Name
{
get { return name; }
set { name = value; }
}

public char Sex
{
get { return sex; }
set { sex = value; }
}

public float Weight
{
get { return weight; }
set { weight = value; }
}
}

请教各位:为什么出现上面如标题所示得问题啊?急.

...全文
1888 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuzhongxf 2008-01-23
  • 打赏
  • 举报
回复
问题解决了吗?
mingjunr 2007-06-13
  • 打赏
  • 举报
回复
选中xml文件右键属性里有
zbluestar 2007-06-13
  • 打赏
  • 举报
回复
写成<class name="QuickStart.Cat" table="Cat">这个也试过了。
怎么才能设为嵌入式资源呢?
mingjunr 2007-06-13
  • 打赏
  • 举报
回复
另外要设为嵌入式资源之类的
mingjunr 2007-06-13
  • 打赏
  • 举报
回复
<class name="Cat" table="Cat">
我一般都写成<class name="QuickStart.Cat" table="Cat">
别处没看出什么不妥来
zbluestar 2007-06-12
  • 打赏
  • 举报
回复
public sealed class NHibernateHelper
{
private const string CurrentSessionKey = "nhibernate.current_session";
private static readonly ISessionFactory sessionFactory;

static NHibernateHelper()
{
Configuration cfg = new Configuration().Configure();
cfg.AddAssembly("QuickStart");
sessionFactory = cfg.BuildSessionFactory();
}

public static ISession GetCurrentSession()
{
HttpContext context = HttpContext.Current;
ISession currentSession = context.Items[CurrentSessionKey] as ISession;
if (currentSession == null)
{
currentSession = sessionFactory.OpenSession();
context.Items[CurrentSessionKey] = currentSession;
}
return currentSession;
}

public static void CloseSession()
{
HttpContext context = HttpContext.Current;
ISession currentSession = context.Items[CurrentSessionKey] as ISession;
if (currentSession == null)
{
// No current session
return;
}
currentSession.Close();
context.Items.Remove(CurrentSessionKey);
}

public static void CloseSessionFactory()
{
if (sessionFactory != null)
{
sessionFactory.Close();
}
}
}



private void Page_Load(object sender, System.EventArgs e)
{
ISession session = NHibernateHelper.GetCurrentSession();
ITransaction tx = session.BeginTransaction();

Cat princess = new Cat();
princess.Name = "Princess";
princess.Sex = 'F';
princess.Weight = 7.4f;

session.Save(princess);
tx.Commit();

NHibernateHelper.CloseSession();
}
canybox 2007-06-12
  • 打赏
  • 举报
回复
多贴点代码啊。。。。
课程通过实际项目融入常用开发技术架构,讲授风格独特,提供详细上课日志及答疑,赠送配套的项目架构源码注释详细清晰且表达通俗,均能直接在实际项目中应用,正真的物超所值,价格实惠任务作业:综合运用《C#/.Net企业级系统架构设计实战精讲教程》课程所学知识技能设计一个学生成绩管理系统的架构。要求:1.系统基于MVC的三层架构,各层单独建不同的解决方案文件夹。2.采用Model First开发方式,设计架构时只需要设计学生表(TbStudent)和课程表(TbCourse)。学生表必须有的字段是ID、stuName、age;课程表必须有的字段是ID、courseName、content。3.数据访问层采用Entity Framework或NHibernate来实现,必须封装对上述表的增删改查方法。4.必须依赖接口编程,也就是必须要有数据访问层的接口层、业务逻辑层的接口层等接口层。层层之间必须减少依赖,可以通过简单工厂或抽象工厂。5.至少采用简单工厂、抽象工厂、Spring.Net等技术中的2种来减少层与层之间的依赖等。6.封装出DbSession类,让它拥有所有Dal层实例和SaveChanges方法。7.设计出数据访问层及业务逻辑层主要类的T4模板,以便实体增加时自动生成相应的类。8.表现层要设计相关的控制器和视图来验证设计的系统架构代码的正确性,必须含有验证增删改查的方法。9.开发平台一定要是Visual Studio平台,采用C#开发语言,数据库为SQL Server。10.提交整个系统架构的源文件及生成的数据库文件。(注意: 作业需写在CSDN博客中,请把作业链接贴在评论区,老师会定期逐个批改~~)

110,552

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧