GenericADOException未处理无法插入

本文关键字:插入 未处理 GenericADOException | 更新日期: 2023-09-27 17:58:02

我正在尝试使用带有ByCode映射的NHibernate,但没有取得多大成功。知道我做错了什么吗?

我的桌子:

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]

我的班级:

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

我的映射类:

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

我用来管理会话和配置的单例:

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;
    }
}

我用来保存活动的代码:

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

Activity.Add上,我得到了这个异常:

GenericADOException未处理:无法插入:
[SimpleTimer.Domain.Activity][SQL:INSERT INTO dbo.Activity([Date],Userid、Customerjob、Service、[Class]、Notes、Billable、Duration)值(;选择SCOPE_IDENTITY()]

知道我做错了什么吗?

GenericADOException未处理无法插入

这类异常通常有一个明确的答案。。。下面几行。我希望有这样的东西:

GenericADOException未处理:无法插入:[SimpleTimer.Domain.Activity][SQL:insert INTO dbo.Activity([Date],Userid,Customerjob,Service,[Class],Notes,Billable,Duration)VALUES(;选择SCOPE_IDENTITY()]
-->System.Data.SqlClient.SqlException:无法将值NULL插入列"ActivityId"、表"DBNAME.dbo.Activity"中;列不允许为null。INSERT失败。。。。

正如我在上面所展示的,最可疑的可能是关闭了IDENTITY

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

或更改发电机:5.1.4.1。生成器