NHibernate + Cannot insert the value NULL into...
- by mybrokengnome
I've got a MS-SQL database with a table created with this code
CREATE TABLE [dbo].[portfoliomanager](
[idPortfolioManager] [int] NOT NULL PRIMARY KEY IDENTITY,
[name] [varchar](45) NULL
)
so that idPortfolioManager is my primary key and also auto-incrementing. Now on my Windows WPF application I'm using NHibernate to help with adding/updating/removing/etc. data from the database. Here is the class that should be connecting to the portfoliomanager table
namespace PortfolioManager
{
[Class(Table="portfoliomanager",NameType=typeof(PortfolioManagerClass))]
public class PortfolioManagerClass {
[Id(Name = "idPortfolioManager")]
[Generator(1, Class = "identity")]
public virtual int idPortfolioManager { get; set; }
[NHibernate.Mapping.Attributes.Property(Name = "name")]
public virtual string name { get; set; }
public PortfolioManagerClass() {
}
}
}
and some short code to try and insert something
PortfolioManagerClass portfolio = new PortfolioManagerClass();
Portfolio.name = "Brad's Portfolios";
The problem is, when I try running this, I get this error:
{System.Data.SqlClient.SqlException: Cannot insert the value NULL into column
'idPortfolioManager', table 'PortfolioManagementSystem.dbo.portfoliomanager'; column
does not allow nulls. INSERT fails. The statement has been terminated...
with an outer exception of
{"could not insert: [PortfolioManager.PortfolioManagerClass][SQL: INSERT INTO
portfoliomanager (name) VALUES (?); select SCOPE_IDENTITY()]"}
I'm hoping this is the last error I'll have to solve with NHibernate just to get it to do something, it's been a long process. Just as a note, I've also tried setting Class="native" and unsaved-value="0" with the same error. Thanks!
Edit:
Ok removing the 1, from Generator actually allows the program to run (not sure why that was even in the samples I was looking at) but it actually doesn't get added to the database. I logged in to the server and ran the sql server profiler tool and I never see the connection coming through or the SQL its trying to run, but NHibernate isn't throwing an error anymore. Starting to think it would be easier to just write SQL statements myself :(