我正在尝试使用带有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()]
有什么迹象表明我做错了什么?
答案 0 :(得分:4)
这种例外通常有明确的答案......下面几行。我希望有类似的东西:
GenericADOException未处理:无法插入:[SimpleTimer.Domain.Activity] [SQL:INSERT INTO dbo.Activity([Date],Userid,Customerjob,Service,[Class],Notes,Billable,Duration)VALUES(? ,?,?,?,?,?,?,?);选择SCOPE_IDENTITY()] ...
... - &gt; System.Data.SqlClient.SqlException:无法将值NULL插入到'ActivityId'列中,表'DBNAME.dbo.Activity';列不允许空值。 INSERT失败 ....
正如我试图在上面展示的那样,最可疑的是IDENTITY正在关闭。只需应用这样的东西
ALTER TABLE [dbo].[Activity] ALTER COLUMN [ActivityId] int NOT NULL IDENTITY(1,1)
或更改生成器:5.1.4.1. generator