13 Replies - 2802 Views - Last Post: 10 April 2010 - 11:28 AM Rate Topic: -----

#1 erburrell  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 22-December 09

Getting an array of the i\Ids in a database with NHibernate

Posted 31 March 2010 - 02:47 PM

All,

I am working with NHibernate and have a question. I have the database gettign information, updating, adding and deleting, but I want to be able to navigate through it. So far, I am struggling with the documentation available and would like to ask if there is an easy way to do it?

I have tried several ways of getting the data through IEnumerable, but can't seem to get my session variable to match up. My database name is burrsoft, my table name is employee and the id field is Id and is int type.


Thanks,

Ed

Is This A Good Question/Topic? 0
  • +

Replies To: Getting an array of the i\Ids in a database with NHibernate

#2 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Getting an array of the i\Ids in a database with NHibernate

Posted 31 March 2010 - 03:01 PM

Hope this helps:

IList<Employee> list = session.CreateCriteria("employee").List<Employee>();


This should get you the complete table of employee records from database using NHibernate.

You can check my tutorial on Fluent NHibernate where you can find some basic queries for NHibernate...

This post has been edited by FlashM: 31 March 2010 - 03:00 PM

Was This Post Helpful? 1
  • +
  • -

#3 erburrell  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 22-December 09

Re: Getting an array of the i\Ids in a database with NHibernate

Posted 01 April 2010 - 11:27 AM

Flash,

Here is what I tried, and I am getting the following error:

Message="No persister for: Employees"

Call from WPF Form:
public Window1()
        {
            InitializeComponent();
            IEmployeesRepository repository = new EmployeesRepository();

            IList<Employees> employeeList = repository.GetEmployees();

            ui_IdComboBox.Items.Clear();
            foreach (Employees employee in employeeList)
            {
                ui_IdComboBox.Items.Add(employee.Id);
            }
        }


This is in my EmployeeRepository:
public IList<Employees> GetEmployees()
        {

            ISession session = NHibernateHelper.OpenSession();
            IList<Employees> employeeList = session.CreateCriteria("Employees").List<Employees>();

            return employeeList;
            
        }  


One question is what is the "Employees" for inside the CreateCriteria for ?

Thanks,

Ed.
Was This Post Helpful? 0
  • +
  • -

#4 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Getting an array of the i\Ids in a database with NHibernate

Posted 01 April 2010 - 04:58 PM

"Employees" inside the CreateCriteria is the name of your table in your database. It has nothing to do with the name of your Employees class in C#.

This kind of error is usually caused by an xml file not being set to "embedded resource", or a typo in the mapping.
Was This Post Helpful? 0
  • +
  • -

#5 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Getting an array of the i\Ids in a database with NHibernate

Posted 02 April 2010 - 11:59 AM

I recall your table name for this being Employee - not Employees.

http://www.dreaminco...post__p__973882
Was This Post Helpful? 1
  • +
  • -

#6 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Getting an array of the i\Ids in a database with NHibernate

Posted 03 April 2010 - 01:44 AM

Good observation MentalFloss! :-) You get my rep point :-)
Was This Post Helpful? 0
  • +
  • -

#7 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Getting an array of the i\Ids in a database with NHibernate

Posted 03 April 2010 - 01:50 AM

View PostFlashM, on 03 April 2010 - 12:44 AM, said:

Good observation MentalFloss! :-) You get my rep point :-)


Hehe thanks. What it really boils down to is I'm probably spending way too much time on the forum.
Was This Post Helpful? 0
  • +
  • -

#8 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Getting an array of the i\Ids in a database with NHibernate

Posted 03 April 2010 - 02:01 AM

hehehe... This sounds so familiar to me... I just woke up (it's 11AM here), went to a local pub for a good morning coffee and first thing I needed to do is to check all the replayed and new topics on DreamInCode :-)
Was This Post Helpful? 0
  • +
  • -

#9 erburrell  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 22-December 09

Re: Getting an array of the i\Ids in a database with NHibernate

Posted 06 April 2010 - 11:55 AM

You guys are obviously working to hard on my problems...:)

I changed the table name when I decided to try out MySQL. I am having trouble figuring out how to configfure Fluent NHibernate to connect to it though. I was using MS SQL Express, and wanted to use a better system. Is SQLLite worth the trouble?

Thanks for all the help!

Ed..
Was This Post Helpful? 0
  • +
  • -

#10 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Getting an array of the i\Ids in a database with NHibernate

Posted 07 April 2010 - 12:48 AM

Didn't use SQLLite before so I really couldn't comment on that... Could you post your code so I can see how you configured your NHibernate configuration. Did you manage to use it with MSSQL Express?
Was This Post Helpful? 0
  • +
  • -

#11 erburrell  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 22-December 09

Re: Getting an array of the i\Ids in a database with NHibernate

Posted 09 April 2010 - 01:12 PM

Ok,Flash,

Here is the program as it is configured:

As always, thanks for the help.

Ed.

BurrSOFT
|--DAO
| |-EmployeesDso.cs
|
|--Entities
| |-Employees.cs
|
|-Window1.xaml

It looks like I am now connecting, but when I try to get an employee from the database, it is null.

Here are my program files:

Employees.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BurrSOFT.Entities
{
    public class Employees
    {
        public virtual int Id { get; set; }
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
        public virtual string Address1 { get; set; }
        public virtual string Address2 { get; set; }
        public virtual string City { get; set; }
        public virtual string State { get; set; }
        public virtual string Zip { get; set; }

    }
}


EmployeesDao.cs:
sing System;
using System.Collections.Generic;
using NHibernate;
using BurrSOFT.Entities;

namespace BurrSOFT.DAO
{
    public class EmployeesDao
    {
        public static Employees Get(ISession session, int Id)
        {
            return session.Get<Employees>(Id);
        }

        public static void Save(ISession session, Employees employee)
        {
            if (employee == null)
                throw new ArgumentNullException("employee must not be null!");

            session.Save(employee);
        }

        public static void Delete(ISession session, Employees employee)
        { 
            if (employee == null )
                throw new ArgumentNullException("employee must not be null!");

            session.Delete(employee);
        }

        public static IList<Employees> ListAll(ISession session)
        {
            return session
                .CreateCriteria(typeof(Employees))
                .List<Employees>();
        }
    }
}



Window1.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FluentNHibernate;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Automapping;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NHibernate.Criterion;
using BurrSOFT.Entities;
using BurrSOFT.DAO;

namespace BurrSOFT
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            ISessionFactory sessionFactory = BuildSessionFactory();
            int id_employee = 1;
            using (ISession session = sessionFactory.OpenSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    Employees emp = session.Get<Employees>(id_employee);
                    FillForm(emp);
                }
            }
            
            
        }

        private void ui_GetButton_Click(object sender, RoutedEventArgs e)
        {
            
        }

        private void ui_RemoveButton_Click(object sender, RoutedEventArgs e)
        {

        }

        private void ui_AddButton_Click(object sender, RoutedEventArgs e)
        {
            
        }

        private void FillForm(Employees employee)
        {
            ui_IdTextBox.Text = employee.Id.ToString();
            ui_FirstNameTextBox.Text = employee.FirstName;
            ui_LastNameTextBox.Text = employee.LastName;
            ui_Address1TextBox.Text = employee.Address1;
            ui_Address2TextBox.Text = employee.Address2;
            ui_CityTextBox.Text = employee.City;
            ui_StateTextBox.Text = employee.State;
            ui_ZipTextBox.Text = employee.Zip;
        }

        private static AutoPersistenceModel CreateMappings()
        {
            return AutoMap
                .Assembly(System.Reflection.Assembly.GetCallingAssembly())
                .Where(t => t.Namespace == "BurrSOFT.Entities");
        }

        private static void BuildSchema(Configuration config)
        {
            new SchemaExport(config).Create(false, true);
        }

        private static ISessionFactory BuildSessionFactory()
        {
            AutoPersistenceModel model = CreateMappings();

            return Fluently.Configure()
                .Database(MySQLConfiguration.Standard
                .ConnectionString(c => c
                    .Server("localhost")
                    .Database("burrsoft")
                    .Username("root")
                    .Password("jsb1srb2")))
                    .Mappings(m => m
                        .AutoMappings.Add(model))
                        .ExposeConfiguration(BuildSchema)
                        .BuildSessionFactory();
        }
        
    }
}



Also, since I will have multiple forms, should I move the session factory,CreateMappings, snf Build Schema to a seperate class file? If so, is there a particular file name/ directory it should have?

Thanks,

Ed.
Was This Post Helpful? 0
  • +
  • -

#12 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Getting an array of the i\Ids in a database with NHibernate

Posted 10 April 2010 - 06:44 AM

I'm looking at it and as soon as I find the reason, I'll let you know...
Was This Post Helpful? 0
  • +
  • -

#13 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Getting an array of the i\Ids in a database with NHibernate

Posted 10 April 2010 - 10:44 AM

There are several things that might be causing this problem, so do the following:

1. Make sure that you have added a reference to the MySql.Data.dll file in your solution explorer.
2. Make sure that you have MySql.Data.dll file in your bin/debug folder (if not, copy it manually or set it's 'Copy local' property on true).

3. Could you update your Windows() construstor to look like this:

public Window1()
{
    InitializeComponent();
    ISessionFactory sessionFactory = BuildSessionFactory();
    int id_employee = 1;

    using (ISession session = sessionFactory.OpenSession())
    {
        using (ITransaction transaction = session.BeginTransaction())
        {
            Employees e = new Employees();
            e.FirstName = "John";
            e.LastName = "Doe";

            EmployeesDao.Save(session, e);
            transaction.Commit();
        }
    }

    using (ISession hs = sessionFactory.OpenSession())
    {
        Employees emp = EmployeesDao.Get(hs, 1);
        FillForm(emp);
    }
}



If you are using a transaction, you need to remember to commit the transaction so that values are actually stored in your database.


4. If you get some weird exception saying 'Column 'Reserved Word' does not belong to table ReservedWords' then update your BuildSchema method to look like this:

private static void BuildSchema(Configuration config)
{
    config.SetProperty(NHibernate.Cfg.Environment.Hbm2ddlKeyWords, "none");
    new SchemaExport(config).Create(false, true);
}




Hope this resolves your problem...

IMPORTANT!!!

Everytime this line of code is called: .ExposeConfiguration(BuildSchema) your database will be re-created, so you will loose all your data that is already stored in your database. Use this line of code only the first time that you want to create your database. Then you can simply remove this line of code from BuildSessionFactory method.

Also make sure that you have created blank/empty database called burrsoft in your MySql server.
Was This Post Helpful? 1
  • +
  • -

#14 erburrell  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 22-December 09

Re: Getting an array of the i\Ids in a database with NHibernate

Posted 10 April 2010 - 11:28 AM

Thanks Flash!

I had not set MySQL.Data.DLL to be coppied to local. That seems to have fixed it. As well as removing .ExposeConfiguration(BuildSchema) :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1