Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GenericADOException was unhandled Could not insert

I am trying to use NHibernate with ByCode mapping and am not having much success. Any idea what I'm doing wrong?

My table:

CREATE TABLE [dbo].[activities](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [date] [bigint] NOT NULL,
    [userId] [int] NOT NULL,
    [customerJob] [int] NULL,
    [service] [int] NULL,
    [class] [int] NULL,
    [notes] [text] NULL,
    [billable] [tinyint] NOT NULL,
    [duration] [int] NOT NULL,
 CONSTRAINT [PK_activities_1] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

My class:

public class Activity {
    public virtual int Id { get; set; }
    public virtual long Date { get; set; }
    public virtual int Userid { get; set; }
    public virtual int? Customerjob { get; set; }
    public virtual int? Service { get; set; }
    public virtual int? Class { get; set; }
    public virtual string Notes { get; set; }
    public virtual byte Billable { get; set; }
    public virtual int Duration { get; set; }

    public static void Add(Activity activity) {
        using (ISession session = ApplicationContextSingleton.SessionFactory.OpenSession()) {
            using (ITransaction transaction = session.BeginTransaction()) {
                session.Save(activity);
                transaction.Commit();
            }
        }
    }
}

My mapping class:

namespace SimpleTimer.Maps
{
    public class ActivitiesMap : ClassMapping<Activity>
    {
        public ActivitiesMap()
        {
            Schema("dbo");
            Lazy(true);
            Id(x => x.Id, map => map.Generator(Generators.Identity));
            Property(x => x.Date, map => map.NotNullable(true));
            Property(x => x.Userid, map => map.NotNullable(true));
            Property(x => x.Customerjob);
            Property(x => x.Service);
            Property(x => x.Class);
            Property(x => x.Notes);
            Property(x => x.Billable, map => map.NotNullable(true));
            Property(x => x.Duration, map => map.NotNullable(true));
        }
    }
}

My singleton I use to manage the session and configuration:

class ApplicationContextSingleton {
    private static Configuration configuration;
    private static ISessionFactory sessionFactory;

    public static Configuration NHConfiguration {
        get {
            if (configuration == null) {
                AppConfigure();
            }
            return configuration;
        }
    }

    public static ISessionFactory SessionFactory {
        get {
            if (sessionFactory == null) {
                AppConfigure();
            }
            return sessionFactory;
        }
    }

    private static void AppConfigure() {
        configuration = ConfigureNHibernate();
        sessionFactory = NHConfiguration.BuildSessionFactory();
    }

    private static Configuration ConfigureNHibernate() {
        // Create the object that will hold the configuration settings
        // and fill it with the information to access to the database
        NHibernate.Cfg.Configuration configuration = new NHibernate.Cfg.Configuration();
        configuration.Properties[NHibernate.Cfg.Environment.ConnectionProvider] = "NHibernate.Connection.DriverConnectionProvider";

        // These are the three lines of code to change in order to use another database
        configuration.Properties[NHibernate.Cfg.Environment.Dialect] = "NHibernate.Dialect.MsSql2000Dialect";
        configuration.Properties[NHibernate.Cfg.Environment.ConnectionDriver] = "NHibernate.Driver.SqlClientDriver";
        configuration.Properties[NHibernate.Cfg.Environment.ConnectionString] = System.Configuration.ConfigurationManager.ConnectionStrings["SimpleTimerDatabase.Properties.Settings.QTimerConnectionString"].ConnectionString;

        var mapping = GetMappings();
        configuration.AddDeserializedMapping(mapping, "NHSchemaTest");
        SchemaMetadataUpdater.QuoteTableAndColumns(configuration);

        return configuration;
    }

    private static HbmMapping GetMappings() {
        var mapper = new ModelMapper();
        mapper.AddMappings(Assembly.GetAssembly(typeof(ActivitiesMap)).GetExportedTypes());
        var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();

        return mapping;
    }
}

The code I'm using to save the Activity:

Activity a = new Activity();
a.Date = DateTime.Now.Ticks / TimeSpan.TicksPerSecond;
a.Userid = 1;
a.Notes = "Notes";
a.Billable = 1;
a.Duration = 60 * 60 * 3; //three hours

Activity.Add(a);
Console.WriteLine("Added Activity");

On Activity.Add I get this exception:

GenericADOException was Unhandled: could not insert:
[SimpleTimer.Domain.Activity][SQL: INSERT INTO dbo.Activity ([Date], Userid, Customerjob, Service, [Class], Notes, Billable, Duration) VALUES (?, ?, ?, ?, ?, ?, ?, ?); select SCOPE_IDENTITY()]

Any clue what I'm doing wrong?

like image 208
Malfist Avatar asked Feb 07 '26 14:02

Malfist


1 Answers

This kind of exceptions usually has a clear answer ... few lines below. And I would expect something like:

GenericADOException was Unhandled: could not insert: [SimpleTimer.Domain.Activity][SQL: INSERT INTO dbo.Activity ([Date], Userid, Customerjob, Service, [Class], Notes, Billable, Duration) VALUES (?, ?, ?, ?, ?, ?, ?, ?); select SCOPE_IDENTITY()] ...
... --> System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'ActivityId', table 'DBNAME.dbo.Activity'; column does not allow nulls. INSERT fails ....

And as I tried to show above, the most suspected would be the IDENTITY beeing turned off. Just apply something like this

ALTER TABLE [dbo].[Activity] ALTER COLUMN [ActivityId] int NOT NULL IDENTITY(1,1)

Or change the Generator: 5.1.4.1. generator

like image 77
Radim Köhler Avatar answered Feb 09 '26 05:02

Radim Köhler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!