zoukankan      html  css  js  c++  java
  • 使用 NHibernate 3.1 的增删改

    题外话:

    虽然微软推出了EF4,但是目前支持EF4的虚拟空间很少,即使有支持的空间,价格也非常昂贵,所以使用NH3.1是比较好的选择,并且NH3.1与EF4一样支持Linq和Lambda表达式。如果对EF4有兴趣的,可以看我之前的一篇博文“Asp.net MVC 2.0 + Unity 2.0(IoC) + EF4.0 实例:RoRoWoBlog 开源项目框架代码

    需要的DLL库文件:

    1、从官方下载NHibernate-3.1.0.GA-bin.zip,需要用到压缩包中的DLL文件如下:

    NHibernate-3.1.0.GA-bin\Required_Bins\Iesi.Collections.dll

    NHibernate-3.1.0.GA-bin\Required_Bins\NHibernate.dll

    NHibernate-3.1.0.GA-bin\Required_For_LazyLoading\LinFu\NHibernate.ByteCode.LinFu.dll

    NHibernate-3.1.0.GA-bin\Required_For_LazyLoading\LinFu\LinFu.DynamicProxy.dll

    2、下载ConfORM1.0.1.4_NH3.1GA.zip,需要用到压缩包中的DLL文件如下:

    ConfORM1.0.1.4_NH3.1GA\ConfOrm.dll

     

    创建实体类:

     

    using System.Collections.Generic; 
    using System.Text; 
    using System; 


    namespace DiPiPi.Infrastructure.Hibernate.Entity {
        
        
    public class BlogTag {
            
    public BlogTag() { }
            
    public virtual int Tag_ID { getset; }
            
    public virtual string TagName { getset; }
            
    public virtual string ArticleIDs { getset; }
        }
    }

     

    增删改操作代码:

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using NHibernate.Dialect;
    using NHibernate.Cfg;
    using NHibernate.Cfg.Loquacious;
    using NHibernate.Cfg.MappingSchema;
    using NHibernate.Driver;
    using NHibernate.Tool.hbm2ddl;

    using NHibernate.ByteCode.LinFu;
    using ConfOrm;
    using ConfOrm.NH;
    using DiPiPi.Infrastructure.Hibernate.Entity;

    namespace DiPiPi.Infrastructure.Hibernate
    {
        
    public static class DBHelper
        {
            
    private const string _ConnectionString =
            
    @"Data Source=localhost;Initial Catalog=DiPiPiDB;Integrated Security=True;Pooling=False";


            
    public static Configuration GetConfiguration()
            {
                
                var configure 
    = new Configuration();

                configure.SessionFactoryName(
    "DiPiPiDB");

                
    //Proxy扩展方法用于配置NHibernate延迟加载的字节码提供程序
                configure.Proxy(p => p.ProxyFactoryFactory<ProxyFactoryFactory>());

                configure.DataBaseIntegration(db 
    =>
                {
                    db.Dialect
    <MsSql2005Dialect>();
                    db.Driver
    <SqlClientDriver>();
                    db.ConnectionString 
    = _ConnectionString;
                });

                
    return configure;

            }

            
    public static HbmMapping GetMapping()
            {
                
                var orm 
    = new ObjectRelationalMapper();
                orm.TablePerClass
    <BlogTag>();
                var mapper 
    = new Mapper(orm);
                
    return mapper.CompileMappingFor(new[] { typeof(BlogTag) });
            }

            
    public static void Test()
            {
                
    //配置NHibernate
                var conf = GetConfiguration();
                
    //在Configuration中添加HbmMapping
                conf.AddDeserializedMapping(GetMapping(), "BlogTag");
                
    //配置数据库架构元数据
                SchemaMetadataUpdater.QuoteTableAndColumns(conf);
                
    //创建数据库架构
                new SchemaExport(conf).Create(falsetrue);
                
    //建立SessionFactory
                var factory = conf.BuildSessionFactory();
                
    //打开Session做持久化数据
                using (var s = factory.OpenSession())
                {
                    
    using (var tx = s.BeginTransaction())
                    {
                        var blogTag 
    = new BlogTag { TagName = "我的测试" };
                        s.Save(blogTag);
                        tx.Commit();
                    }
                }

                
    //查询、排序
                using (var s = factory.OpenSession())
                {
                    var query 
    = s.QueryOver<BlogTag>()
                    .Where(p 
    => p.TagName == "我的测试")
                    .OrderBy(p 
    => p.Tag_ID).Asc
                    .List();
                }

                
    //打开Session做删除数据
                using (var s = factory.OpenSession())
                {
                    
    using (var tx = s.BeginTransaction())
                    {
                        s.CreateQuery(
    "delete from BlogTag").ExecuteUpdate();
                        tx.Commit();
                    }
                }
                
    //删除数据库架构
                new SchemaExport(conf).Drop(falsetrue);

            }

        }
    }

     另外值得一提的是,new SchemaExport(conf).Create(false, true); 这里是先写实体代码再创建的数据库哦,呵呵 ^_^,大家一直追求的 Code First 哈。

     

     

     

  • 相关阅读:
    大数据之路Week08_day02 (Flume架构介绍和安装)
    Hive调优
    hive的shell用法(脑子糊涂了,对着脚本第一行是 #!/bin/sh 疯狂执行hive -f 结果报错)
    Week08_day01 (Hive 自定义函数 UDF 一个输入,一个输出(最常用))
    Week08_day01 (Hive开窗函数 row_number()的使用 (求出所有薪水前两名的部门))
    Week08_day01 (Hive实现按照指定格式输出每七天的消费平均数)
    Week08_day01 (Hive实现WordCount计数)
    SQL中 count(*)和count(1)的对比,区别
    大数据之路week07--day07 (修改mysql默认编码)
    10进制转换成16进制的函数(自写函数模板)
  • 原文地址:https://www.cnblogs.com/taven/p/2009847.html
Copyright © 2011-2022 走看看