user1638121
user1638121

Reputation: 167

NHibernate could not insert

Hi i Create empty local database called baza.sdf

My Domain

namespace mapowanie.Domain {
    public class User
    {
        public virtual Guid Id { get; set; }
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
    } }

My Mapping hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="mapowanie"
                   namespace="mapowanie.Domain">

  <class name="User">
    <id name="Id" generator="guid" />
    <property name="FirstName" type="string" />
    <property name="LastName" type="string"/>
  </class>
</hibernate-mapping>

Hibernate.cfg.xml

<?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="dialect">NHibernate.Dialect.MsSqlCeDialect</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlServerCeDriver</property>
    <property name="connection.connection_string">Data Source=D:\aaawypociny\mapowanie\mapowanie\baza.sdf</property>
    <property name="show_sql">true</property>
  </session-factory>
</hibernate-configuration>

UserRepo

class UserRepository
    {
        public void Add(User newUser)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Save(newUser);
                    transaction.Commit();

                }
            }
        }
    }

At transaction.Commit when i try to add

UserRepository repo = new UserRepository();
var ja = new User { FirstName = "Name", LastName = "Surname" };
repo.Add(ja);

VS give me

An unhandled exception of type 'NHibernate.Exceptions.GenericADOException' occurred in NHibernate.dll

Additional information: could not insert: [mapowanie.Domain.User#8cafd9e5-0eda-4ccc-ba4a-f8935b68dfdc][SQL: INSERT INTO User (FirstName, LastName, Id) VALUES (?, ?, ?)]

what i can do wrong?

Upvotes: 2

Views: 6214

Answers (3)

Terry Kernan
Terry Kernan

Reputation: 806

In some cases where the actual exception message isn't being shown, you could try:

  • checking that all the fields are nullable in the db so that if some required values aren't being supplied, you can see what is going through, you can always change that back later

  • check that your field lengths are large enough to hold the data that is being sent, otherwise you'll get a data will be truncated message in the guise of a could not insert message

Upvotes: 0

Manoj Patil
Manoj Patil

Reputation: 980

Felice Pollano was right regarding reserved word "User", please rename the table and try again. I faced same issue renaming solved my issue.

Also please verify table name you passed is correct, I didnt renamed that, I struggled for it..

<class name="User1" table="User1" lazy="false">

Upvotes: 0

Felice Pollano
Felice Pollano

Reputation: 33252

You have to escape the table name, I think "User" is a suspect reserved word., try to define in the hbm:

<class name="User" table="`User`">

note the `` around user, this notify NHibernate to escape the table name. To use issue queries by hand ( ie not through NHibernate ) escape the name manually, for example:

create table [User] (
    Id UNIQUEIDENTIFIER not null,
....

Upvotes: 1

Related Questions