当我尝试在WPF + C#应用程序上使用NHibernate保存实例时,我遇到了问题。
调试它,显示步骤session.Save(MyInstance);
上发生的错误
您可以在下面看到完整的方法。错误是:
未处理的类型异常 ' NHibernate.Exceptions.GenericADOException'发生在NHibernate.dll中 附加信息:无法插入:[Blog.Models.Province] [SQL: INSERT INTO [省](CodProv,NomeProv,Regiao)VALUES(?,?,?); 选择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();
}
NHibernateHelper类
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();
}
}
有人可以帮助我吗?
这是我的省级和制图班级
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; }
}
映射类
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);
}
}
如有必要,请点击我的表省份脚本。
CREATE TABLE Provincia(
ID INT IDENTITY(1,1) PRIMARY KEY,
codProv VARCHAR(2) NOT NULL,
nomeProv VARCHAR(25) NOT NULL,
regiao VARCHAR(10) );
答案 0 :(得分:1)
这是我的配置: 我有2个connectionString 1.名称Blog2 - &gt;我用来连接本地数据库(mdf) 2.名称博客 - &gt;我用来连接SQL Server DB(dbo)
NHibernate只能连接到本地数据库!
<?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>
答案 1 :(得分:0)
基于映射
与更新的问题相关,映射中存在一个不一致,请检查:
[SQL: INSERT INTO
[Province]
(
CREATE TABLE
Provincia
(
所以,如果问题中没有拼写错误,问题很明显,没有表省 ...只是 Provincia
原始部分
NHibernate异常是真正的宝藏,因为详细描述了所有秘密。您的异常,完整堆栈跟踪,很可能如下所示:
// YOUR part
NHibernate.dll中发生了'NHibernate.Exceptions.GenericADOException'类型的未处理异常附加信息:无法插入:[Blog.Models.Province] [SQL:INSERT INTO [省](CodProv,NomeProv,Regiao)VALUES (?,?,?);选择SCOPE_IDENTITY()]
// next part - inner exception
---&GT; System.Data.SqlClient.SqlException:无法将值NULL插入列'省_ID',表'省';列不允许空值。 INSERT失败。
它甚至可能是另一个内部异常,但从它的描述中你现在就会清楚。
如果问题是 "column does not allow nulls"
,则意味着我们期望:
Id(x => x.Id, "Province_ID")
.GeneratedBy.Identity()
...
而DB 不设置为:
[Province_ID] [int] IDENTITY(1,1) NOT NULL,
请确保您的ID列已定义IDENTITY,请将其更改为...