vvoodrovv
vvoodrovv

Reputation: 478

Couldn't insert into a table using nhibernate

I'm just learning nhibernate and trying to create the first sample.

I created an empty service-based database and wrote mappings and some code:

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();

When I run this I always get the error could not insert:

INSERT INTO Skill (Description) VALUES (?); 
select SCOPE_IDENTITY()

Invalid object name \"Skill\".

I didn't created tables as nhibernate should create them itself.

Could you help me to figure out why it is not working?

Upvotes: 4

Views: 2473

Answers (2)

StepUp
StepUp

Reputation: 38094

This solution works, however it clears all my data from another table. I've found a root of an occurred exception: check your "Id" column in the table. The 'Id' column should be incremented. Under the creation a table it is necessary to set IDENTITY to unique field like that:

CREATE TABLE dbo.aTable (ID_User INTEGER NOT NULL PRIMARY KEY IDENTITY(1,1), Name VARCHAR(30)) END

Upvotes: 0

mickfold
mickfold

Reputation: 2003

To get NHibernate to create the DB schema add the following line before the SessionFactory is built:

new NHibernate.Tool.hbm2ddl.SchemaExport(configuration)
                                            .Execute(false, true, false);

The signature for this method is

public void Execute(bool script, bool export, bool justDrop);
  • script - If this is true then the generated schema script is written to the Console.
  • export - If this is true then the generated schema script is ran against the configured DB.
  • justDrop - If this is true then the tables that are always dropped.

More information about the export schema feature can be found in the NH docs.

Upvotes: 3

Related Questions