zoukankan      html  css  js  c++  java
  • 记录Castle ActiveRecord访问Sqlite的配置

    1、ActiveRecord配置文件ARConfig.xml,并将配置文件的“生成操作”改成“嵌入的资源”

    <?xml version="1.0" encoding="utf-8" ?>
    <activerecord isWeb="true">
      <config>
        <add key="connection.driver_class" value="NHibernate.Driver.SQLite20Driver" />
        <add key="dialect" value="NHibernate.Dialect.SQLiteDialect" />
        <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
        <add key="connection.connection_string" value="data source=|DataDirectory|message.db;Version=3;" />
        <add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"/>
        <add key="show_sql" value="true"/>
      </config>
    </activerecord>

    2、使用到的DLL文件

    3、测试实体类UserInfo

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Castle.ActiveRecord;
    using NHibernate;

    namespace CY.Domain
    {
        [ActiveRecord("T_User")]
        public class UserInfo : ActiveRecordBase
        {
            #region 属性

            [PrimaryKey(PrimaryKeyType.Identity, "ID")]
            public int ID { getset; }

            [Property("UserName")]
            public string Name { getset; }

            [Property("Password")]
            public string Password { getset; }

            [Property("RegTime")]
            public DateTime RegTime { getset; }

            #endregion

            #region 方法

            public static void Insert(UserInfo user)
            {
                Save(user);
            }

            public static IList<UserInfo> Select()
            {
                return (IList<UserInfo>)FindAll(typeof(UserInfo));
            }

            public static UserInfo Query(int id)
            {
                object model = FindByPrimaryKey(typeof(UserInfo), id);

                return model != null ? model as UserInfo : null;
            }

            public static void Del(int id)
            {
                UserInfo model = Query(id);
                if (model != null)
                {
                    Delete(model);
                }
            }
           
            #endregion
        }
    }
    View Code

    4、初始化实体类对象(Global.asax)

    using Castle.ActiveRecord;

    using Castle.ActiveRecord.Framework.Config;

            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();

                RegisterRoutes(RouteTable.Routes);

                XmlConfigurationSource source = new XmlConfigurationSource(Server.MapPath("ARConfig.xml"));
                ActiveRecordStarter.Initialize(source, typeof(UserInfo));
            }

    5、使用

                for (int i = 0; i < 10; i++)
                {
                    UserInfo.Insert(new UserInfo()
                    {
                        Name = "user" + i.ToString(),
                        Password = "123456",
                        RegTime = DateTime.Now
                    });
                }

                IList<UserInfo> models = UserInfo.Select();
                ViewData["users"] = models;
  • 相关阅读:
    DAS存储未死,再次欲获重生
    Minimum edit distance(levenshtein distance)(最小编辑距离)初探
    AC自己主动机
    手动脱UPX 壳实战
    edge中断分析
    ubuntu默认的Python版本号修改
    Linux 下 pushd,popd,cd- 用法
    目标检测算法的历史及分类
    openjtag 的硬件连接踩坑历程
    ubuntu 16.04 python版本切换(python2和python3)
  • 原文地址:https://www.cnblogs.com/wanfeng/p/3160006.html
Copyright © 2011-2022 走看看