Fluent Hibernate插入失败,不能将NULL值插入到'Id'列中, [英] Fluent Hibernate Insert Fail, Cannot insert the value NULL into column 'Id',

查看:432
本文介绍了Fluent Hibernate插入失败,不能将NULL值插入到'Id'列中,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这就是它在Repository(自定义)层中调用的方法

'pre> 私人无效AddOrUpdateObject(对象objectToUpdate)
{
UnitOfWork.CurrentSession.SaveOrUpdate(objectToUpdate);





$这是生成的SQL语句,我在日志中发现它们



2013-07-08 15:28:43,919 [DEBUG] NHibernate.SQL - INSERT INTO [Season](Name,StartDate,EndDate,ModifiedDate)VALUES(@ p0,@ p1,@ p2,@ p3);选择SCOPE_IDENTITY(); @ p0 ='season 4'[Type:String(4000)],@ p1 = 6/13/2014 12:00:00 AM [Type:DateTime(0)],@ p2 = 12/31 / 2013 12:00:00 AM [Type:DateTime(0)],@ p3 = 7/8/2013 3:28:43 PM [Type:DateTime(0)]



这是我得到的异常


$ b 2013-07-08 15:28:43,929 [WARN NHibernate.Util .ADOExceptionReporter - System.Data.SqlClient.SqlException(0x80131904):无法将值NULL插入列'Id',表'ticketing.social.dbo.Season';列不允许空值。 INSERT失败。

为什么不能在表格中插入新的一行Season,即使它使用了select SCOPE_IDENTITY();


而不是CurrentSession.SaveorUpdate(),还有另外一种方法可以从ISession中使用吗?



请注意,很多由于这是一个SQL异常,并且您的查询显示正常,所以您的ORM似乎设置正确,但是,你的数据库不是。我敢打赌你的专栏实际上并不是数据库中的 IDENTITY 列。


I am getting an exception on INSERT statement to table using NHibernate

This is how it is called in the Repository (custom) layer

private void AddOrUpdateObject(object objectToUpdate)
{
    UnitOfWork.CurrentSession.SaveOrUpdate(objectToUpdate);
}

This is the SQL Statement generated, i found them in the log

2013-07-08 15:28:43,919 [36] DEBUG NHibernate.SQL - INSERT INTO [Season] (Name, StartDate, EndDate, ModifiedDate) VALUES (@p0, @p1, @p2, @p3); select SCOPE_IDENTITY();@p0 = 'season 4' [Type: String (4000)], @p1 = 6/13/2014 12:00:00 AM [Type: DateTime (0)], @p2 = 12/31/2013 12:00:00 AM [Type: DateTime (0)], @p3 = 7/8/2013 3:28:43 PM [Type: DateTime (0)]

This is the exception I am getting

2013-07-08 15:28:43,929 [36] WARN NHibernate.Util.ADOExceptionReporter - System.Data.SqlClient.SqlException (0x80131904): Cannot insert the value NULL into column 'Id', table 'ticketing.social.dbo.Season'; column does not allow nulls. INSERT fails.

Why can't it insert a new row to the table "Season", even if it is using select SCOPE_IDENTITY();

Instead of CurrentSession.SaveorUpdate(), is there another method I could use from ISession?

Please advise, many thanks

解决方案

Since this is a SQL exception, and your query appears ok, it appears that your ORM is set up correctly, but your database is not. I'd bet your column is not actually an IDENTITY column in the database.

这篇关于Fluent Hibernate插入失败,不能将NULL值插入到'Id'列中,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆