• NHibernate学习笔记(1) —— 使用NHibernate中应注意的几个小问题


    第一个NHibernate程序测试了用实体生成数据库表和数据添加,总结出以下几点需要注意:

    1.app.config或web.config中要添加,版本的兼容性配置。

    内容:

    <runtime>
            
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
                
    <dependentAssembly>
                    
    <assemblyIdentity name="NHibernate" publicKeyToken="AA95F207798DFDB4" culture="neutral"/>
                    
    <bindingRedirect oldVersion="0.0.0.0-2.1.2.4000" newVersion="2.1.2.4000"/>
                
    </dependentAssembly>
            
    </assemblyBinding>
        
    </runtime>

    为什么要添加,理解不透,我没有把NHibernate程序集添加到GAC,机器上也只有这一个版本的NHibernate,不知道为什么还要写这个,但是不写,就会出现“找不到程序集”之类的错误。
    2.hibernate.cfg.xml配置(这里MS ACCESS 数据库为例,自己到svn下载编译了NHibernate.JetDriver.JetDriver项目)

    内容:

    代码
    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
      
    <session-factory>
        
    <property name="connection.release_mode">on_close</property>
        
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
        
    <property name="dialect">NHibernate.JetDriver.JetDialect, NHibernate.JetDriver</property>
        
    <property name="connection.driver_class">NHibernate.JetDriver.JetDriver, NHibernate.JetDriver</property>
        
    <property name="connection.connection_string">Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\..\..\Db\NHibernateFirst.mdb;</property>
        
    <property name="show_sql">true</property>
        
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle</property>

      
    </session-factory>
    </hibernate-configuration>

    3.控制台测试项目,需要引用的程序集

    NHibernate.dll 这个不用说了,肯定要用。
    NHibernate.ByteCode.Castle.dll ,如果用其他NHibernate支持的DynamicProxy,那要用NHibernate.ByteCode.xxx.dll ,这里用了Castle。
    不引用出现的异常:

    代码
    Unable to load type 'NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle' during configuration of proxy factory class.
    Possible causes are:
    - The NHibernate.Bytecode provider assembly was not deployed.
    - The typeName used to initialize the 'proxyfactory.factory_class' property of the session-factory section is not well formed.

    Solution:
    Confirm that your deployment folder contains one of the following assemblies:
    NHibernate.ByteCode.LinFu.dll
    NHibernate.ByteCode.Castle.dll

    NHibernate.JetDriver.dll,使用了NHibernate.dll没有内置的数据库驱动,要引用相应的程序集。
    不引用出现异常:

    Could not compile the mapping document: NHibernateFirst.Mappings.Product.hbm.xml

    要用到不过不用在项目中添加引用的程序集还有:Iesi.Collections.dll、log4net.dll、Castle.DynamicProxy2.dll、Castle.Core.dll、Antlr3.Runtime.dll

    4.测试代码

    代码
    using System;
    using System.Collections.Generic;
    using System.Text;

    using NHibernateFirst.Domain;
    using NHibernate.Cfg;
    using NHibernate.Tool.hbm2ddl;
    using NHibernateFirst.Repositories;
    using NHibernate;

    namespace NHibernateFirst.Console
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                Configuration configuration 
    = new Configuration();
                configuration.Configure();
                configuration.AddAssembly(
    typeof(Product).Assembly);
                
    new SchemaExport(configuration).Execute(falsetruefalse);
                ISessionFactory sessionFactory 
    = configuration.BuildSessionFactory();
                
    using (ISession session = sessionFactory.OpenSession())
                {
                    Product product;
                    
    string instr;
                    
    while ((instr = System.Console.ReadLine()) != "x")
                    {
                        product 
    = new Product { Name = "Apple", Category = instr };
                        IProductRepository repository 
    = new ProductRepository();
                        session.Save(product);
                    }
                }
                System.Console.Read();
            }
        }
    }
  • 相关阅读:
    第八周作业
    第八周上机练习
    第七周上机练习
    第六周作业
    第六周上机练习
    第五周上机练习
    第四周作业
    第四次上机作业
    第三周作业
    第一次上机作业
  • 原文地址:https://www.cnblogs.com/llcto/p/1742635.html
Copyright © 2020-2023  润新知