single table example - 4 of 4

<?xml version="1.0" encoding="utf-8" ?>

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="TstDBConnection.Entities" assembly="TstDBConnection">

  <class name="Course" table="Course">

    <id name="Id" column="Id">

      <generator class="guid.comb"/>

    </id>

    <property name="Name" column="Name"></property>

    <property name="CreatedDate" column="CreatedOn"></property>

  </class>

</hibernate-mapping>
namespace TstDBConnection.Entities

{

    public class Course

    {

        public virtual Guid Id { get; set; }

        public virtual string Name { get; set; }

        public virtual DateTime CreatedDate { get; set; }

    }

}
        public static void TestSingleTable(ISessionFactory sessionFactory)

        {

            //Use NHibernate to create an entity and get a list of all entities

            using (ISession session = sessionFactory.OpenSession())

            {

                Course emp = new Course()

                {

                    Name = "English",

                    CreatedDate = DateTime.Now

                };

                session.Save(emp);

                session.Flush();



                var query = from course in session.Query<Course>()

                            select course;

                IList<Course> courses = query.ToList();

            }

        }
        static void Main(string[] args)

        {

            log4net.Config.XmlConfigurator.Configure(); // log4net

            Configuration configuration = new Configuration();

            configuration.Configure();

            ISessionFactory sessionFactory = configuration.BuildSessionFactory();



            TestSingleTable(sessionFactory);



            sessionFactory.Close();

        }

你可能感兴趣的:(example)