在C#中使用NHibernate的员工应用程序 [英] Employee Application using NHibernate in C#
问题描述
您好我第一次开始使用NHibernate并使用Code Project中的一些参考。我创建了一个控制台应用程序,可以简单地完成员工详细信息维表Employee有ID和Name作为列,我正在通过Nhibernate删除,插入和更新。但我正面临一个问题或异常对象参考没有设置为一个对象的实例
现在我已经完成了
Employee.cs
Hi I''ve started working with NHibernate for the first time and doing with some reference given in Code Project. I have created a console application which does simple work employee details maintenance. Table Employee has ID and Name as columns, I''m deleting, inserting and updatinh through Nhibernate. But I''m facing an issue or exception Object Reference not set to an instance of an object
HEre is wat I''ve done so far
Employee.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NHibernateSimpleDemo
{
class Employee
{
public virtual int ID { get; set; }
public virtual string Name { get; set; }
}
}
Employee.hbm.xml
Employee.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="NHibernateSimpleDemo" assembly="NHibernateSimpleDemo">
<class name="Employee" table="Employee" lazy="false">
<id name="ID" column="ID">
<column name ="ID"/>
<generator class="native"/>
</id>
<property name="Name" column="Name" />
</class>
</hibernate-mapping>
应用程序配置文件
App config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Data Source=(local);Initial Catalog=Testing;User ID=sa;Password=Keane123"</property>
<property name="show_sql">true</property>
<mapping assembly="NHibernateSimpleDemo" />
</session-factory>
</hibernate-configuration>
</configuration>
<!--http://www.codeproject.com/script/Articles/ViewDownloads.aspx?aid=222279-->
Program.cs文件
Program.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using NHibernate;
using NHibernate.Cfg;
namespace NHibernateSimpleDemo
{
class Program
{
private static Configuration myConfiguration;
private static ISessionFactory mySessionFactory = null;
private static ISession mySession;
static void Main(string[] args)
{
try
{
if (mySession != null && mySession.IsOpen)
{
mySession.Close();
}
if (mySessionFactory != null && !mySessionFactory.IsClosed)
{
mySessionFactory.Close();
}
Employee emp = new Employee();
//emp.ID = 1;
//emp.Name = "Admin";
myConfiguration = new Configuration();
myConfiguration.Configure();
myConfiguration.AddAssembly("NHibernateSimpleDemo");
mySessionFactory = myConfiguration.BuildSessionFactory();
mySession = mySessionFactory.OpenSession();
Console.WriteLine("Insert an employee");
Console.WriteLine("Enter employee ID");
emp.ID = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("enter name of employee");
emp.Name = Console.ReadLine();
using (mySession.BeginTransaction())
{
mySession.Save(emp.ID, emp.Name);
mySession.Transaction.Commit();
}
Console.WriteLine("Update an employee");
using (mySession.BeginTransaction())
{
var empl = mySession.Load<Employee>(4);
empl.Name = "User";
mySession.Update(empl);
mySession.Transaction.Commit();
}
using (mySession.BeginTransaction())
{
Console.WriteLine("Delete an employee");
Console.WriteLine("Enter employee ID");
emp.ID = Convert.ToInt32(Console.ReadLine());
Object result = mySession.CreateSQLQuery("DELETE from Employee where ID = :itm")
.AddScalar("count", NHibernate.NHibernateUtil.Int32)
.SetParameter("itm",emp.ID)
.UniqueResult();
mySession.Transaction.Commit();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message + e.InnerException.Message);
}
}
}
}
我得到这个例外当断点到达时
I''m getting this exception when the break point reaches
mySessionFactory = myConfiguration.BuildSessionFactory();
有人帮助我,我真的很想解决问题
提前致谢
Someone help me I''m really breaking my heads for solution
Thanks in advance
推荐答案
可以将类声明为公共
这篇关于在C#中使用NHibernate的员工应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!