Reputation: 75
I'm experiencing a problem, when I try to save a instance using NHibernate on a WPF + C# application.
Debugging it, show that the errors occurs on step session.Save(MyInstance);
You can see the complete method below. The error is:
An unhandled exception of type 'NHibernate.Exceptions.GenericADOException' occurred in NHibernate.dll Additional information: could not insert: [Blog.Models.Province][SQL: INSERT INTO [Province] (CodProv, NomeProv, Regiao) VALUES (?, ?, ?); select SCOPE_IDENTITY()]
private void txt_Click(object sender, RoutedEventArgs e)
{
using(ISession session = NHibernateHelper.AbreSession())
{
Province province = new Province();
province.CodProv = "11";
province.NomeProv = "Maputo Cidade";
province.Regiao = "Sul";
ITransaction tx = session.BeginTransaction();
session.Save(province);
tx.Commit();
}
The NHibernateHelper Class
public class NHibernateHelper
{
private static ISessionFactory factory = CriaSessionFactory();
private static ISessionFactory CriaSessionFactory()
{
Configuration cfg = new Configuration();
cfg.Configure();
ISessionFactory factory = Fluently.Configure(cfg)
.Mappings(x =>
{
x.FluentMappings.AddFromAssembly(
Assembly.GetExecutingAssembly());
}).BuildSessionFactory();
return factory;
}
public static ISession AbreSession()
{
return factory.OpenSession();
}
}
Someone can help me?
Here's my Province and Mapping classes
public class Province
{
public virtual int Id { get; set; }
public virtual string CodProv { get; set; }
public virtual string NomeProv { get; set; }
public virtual string Regiao { get; set; }
}
Mapping class
public class ProvinceMapping : ClassMap<Province>
{
public ProvinceMapping()
{
Id(province => province.Id).GeneratedBy.Identity();
Map(province => province.CodProv);
Map(province => province.NomeProv);
Map(province => province.Regiao);
}
}
If necessary, here's my table province script.
CREATE TABLE Provincia(
ID INT IDENTITY(1,1) PRIMARY KEY,
codProv VARCHAR(2) NOT NULL,
nomeProv VARCHAR(25) NOT NULL,
regiao VARCHAR(10) );
Upvotes: 0
Views: 1415
Reputation: 75
Here is my config: I have 2 connectionString 1.With name Blog2 -> that I use to connect the Local DB (mdf) 2. Name Blog - > I use to connect SQL Server DB (dbo)
NHibernate only works connected to a local DB!
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name="blog2" providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Elmodai\documents\visual studio 2013\Projects\Blog\Blog\DB1.mdf;Integrated Security=True"/>
<add name="blog" providerName="System.Data.SqlClient"
connectionString="Data Source=ELMODAI-PC;Initial Catalog=VamosVer;Integrated Security=True"/>
</connectionStrings>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</property>
<property name="dialect">
NHibernate.Dialect.MsSql2012Dialect
</property>
<property name="connection.driver_class">
NHibernate.Driver.SqlClientDriver
</property>
<property name="connection.connection_string_name">
blog
</property>
<property name="show_sql">
ture
</property>
<property name="format_sql">
ture
</property>
<property name="hbm2ddl.auto">
update
</property>
</session-factory>
</hibernate-configuration>
</configuration>
Upvotes: 1
Reputation: 123901
BASED ON THE MAPPING
Related to updated question, there is one inconsistence in the mapping, check the:
[SQL: INSERT INTO
[Province]
(
CREATE TABLE
Provincia
(
So, if there is no typo in the question, the problem is clear, There is no table Province... just Provincia
ORIGINAL PART
NHibernate exceptions are the real treasure, because there are all the secrets described in detail. Your exceptions, the full stack trace, most likely looks like this:
// YOUR part
An unhandled exception of type 'NHibernate.Exceptions.GenericADOException' occurred in NHibernate.dll Additional information: could not insert: [Blog.Models.Province][SQL: INSERT INTO [Province] (CodProv, NomeProv, Regiao) VALUES (?, ?, ?); select SCOPE_IDENTITY()]
// next part - inner exception
---> System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'Province_ID', table 'Province'; column does not allow nulls. INSERT fails.
It could be even another inner exception, but from its description you will clearly now.
In case, that the issue is "column does not allow nulls"
it means, that we expect:
Id(x => x.Id, "Province_ID")
.GeneratedBy.Identity()
...
while DB is not set as:
[Province_ID] [int] IDENTITY(1,1) NOT NULL,
Just be sure that your ID column has the IDENTITY defined, ALTER it to have it...
Upvotes: 0