简体   繁体   中英

How get data from table with NHibernate by where?

I used in my project NHibernate like this:

public class DBHelper
    {
        private static ISessionFactory sessions;

        public static void Configure()
        {            
            sessions = new Configuration().Configure().AddClass(typeof(Clients)).BuildSessionFactory();
            //ISessionFactory factory = Fluently.Configure().Mappings(m => m.FluentMappings.AddFromAssemblyOf<Clients>()).Database(MsSqlConfiguration.MsSql2005.ConnectionString(c => c.FromConnectionStringWithKey(DBConnection.GetConnectionString())).BuildSessionFactory();
        }

        public static void Insert(Clients pb)
        {            
            using (ISession session = sessions.OpenSession())           
            using (ITransaction tx = session.BeginTransaction())
            {                
                session.Save(pb);                
                tx.Commit();
            }
        }

        public static void UpdateContact(Clients pb)
        {
            using (ISession session = sessions.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Update(pb);
                tx.Commit();
            }
        }

        public static void DeleteContact(Clients pb)
        {
            using (ISession session = sessions.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Delete(pb);
                tx.Commit();
            }
        }
    }

I have tree simple methods: insert, delete, save. Now I need to get all data from DB table or some data like analog select * from Clients where id='...'

Your options are:

Personally, I really like the QueryOver API:

var clients = session.QueryOver<Clients>().Where(x => x.Name == "Foobar").List();

If you only need to get an entity by its ID, you can use Get or Load

var client = session.Get<Clients>(5);

If you search for NHibernate tutorials you'll certainly find a lot of information. I've heard good things about the Summer Of NHibernate screencasts , though I personally haven't seen them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM