Hi,
I am new to NHibernate, and trying to implement it in my project. While I am trying to create a small application from quickstart. I am getting an error at session.Save(obj). I am doing it in VS 2005.
I changed the myobj.nbt.xml file properties to "Embedded Resource", even though the same error is comming.
please help me, this will make to learn and use new technology in my project.
Here is my Code.
//Consol application class file
using System;
using System.Text;
using NHibernate;
using NHibernate.Cfg;
namespace HibernateTest
{
public class Program
{
static void Main(string[] args)
{
Configuration cfg = new Configuration();
cfg.AddAssembly("HibernateTest");
ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
ITransaction transaction = session.BeginTransaction();
HibernateTest.User newUser = new HibernateTest.User();
newUser.Id = "joe_cool";
newUser.UserName = "Joseph Cool";
newUser.Password = "abc123";
newUser.EmailAddress = "joe@cool.com";
newUser.LastLogon = DateTime.Now;
// Tell NHibernate that this object should be saved
session.SaveOrUpdate(newUser);
// commit all of the changes to the DB and close the ISession
transaction.Commit();
session.Close();
System.Diagnostics.Debug.WriteLine("User Added");
}
}
}
//Entity Class File
using System;
using System.Collections.Generic;
using System.Text;
namespace HibernateTest
{
public class User
{
private string id;
private string userName;
private string password;
private string emailAddress;
private DateTime lastLogon;
public User()
{
}
public virtual string Id
{
get { return id; }
set { id = value; }
}
public string UserName
{
get { return userName; }
set { userName = value; }
}
public string Password
{
get { return password; }
set { password = value; }
}
public string EmailAddress
{
get { return emailAddress; }
set { emailAddress = value; }
}
public DateTime LastLogon
{
get { return lastLogon; }
set { lastLogon = value; }
}
}
}
//MyObj.nbt.xml File
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="User" table="users">
<id name="Id" column="LogonId" type="String" length="20">
<generator class="assigned" />
</id>
<property name="UserName" column="Name" type="String" length="40"/>
<property name="Password" type="String" length="20"/>
<property name="EmailAddress" type="String" length="40"/>
<property name="LastLogon" type="DateTime"/>
</class>
</hibernate-mapping>
//app.config.file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section
name="nhibernate"
type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
/>
</configSections>
<nhibernate>
<add
key="hibernate.connection.provider"
value="NHibernate.Connection.DriverConnectionProvider"
/>
<add
key="hibernate.dialect"
value="NHibernate.Dialect.MsSql2000Dialect"
/>
<add
key="hibernate.connection.driver_class"
value="NHibernate.Driver.SqlClientDriver"
/>
<add
key="hibernate.connection.connection_string"
value="Data Source=B4B-2F-258-8GVX\SQLEXPRESS;Initial Catalog=NHibernate;Integrated Security=True;Pooling=False"
/>
</nhibernate>
</configuration>
_________________ Thanks & Regards
Babar Shaik.
|