我只是在学习 nhibernate 并尝试创建第一个示例。
我创建了一个空的基于服务的数据库并编写了映射和一些代码:
namespace lab
{
public class Skill
{
public virtual int Id { get; protected set; }
public virtual string Description { get; set; }
}
}
<?xml version='1.0' encoding='utf-8'?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="lab" assembly="lab">
<class name="Skill" table="Skill">
<id name="Id">
<generator class="native"/>
</id>
<property name="Description" />
</class>
</hibernate-mapping>
<?xml version='1.0' encoding='utf-8'?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="connection.connection_string">Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Dmitriy\Desktop\lab\lab\lab\DataBase.mdf;Integrated Security=True;User Instance=True</property>
<mapping file="C:\Users\Dmitriy\Desktop\lab\lab\lab\Skill.hbm.xml" />
</session-factory>
</hibernate-configuration>
var configuration = new Configuration();
configuration.Configure(@"C:\Users\Dmitriy\Desktop\lab\lab\lab\hibernate.cfg.xml");
var session = configuration.BuildSessionFactory().OpenSession();
var transaction = session.BeginTransaction();
var skill = new Skill {Description = "C#"};
session.Save(skill);
transaction.Commit();
当我运行它时,我总是得到错误无法插入:
INSERT INTO Skill (Description) VALUES (?);
select SCOPE_IDENTITY()
对象名称“技能”无效。
我没有创建表,因为 nhibernate 应该自己创建它们。
你能帮我弄清楚为什么它不起作用吗?