• 记录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;
  • 相关阅读:
    枚举
    log4j 简单应用
    [luogu4728 HNOI2009] 双递增序列 (dp)
    [luogu3760 TJOI2017] 异或和(树状数组)
    [luogu1485 HNOI2009] 有趣的数列 (组合数学 卡特兰数)
    [luogu4054 JSOI2009] 计数问题(2D BIT)
    [luogu2594 ZJOI2009]染色游戏(博弈论)
    [luogu2591 ZJOI2009] 函数
    [luogu2148 SDOI2009] E&D (博弈论)
    [luogu2154 SDOI2009] 虔诚的墓主人(树状数组+组合数)
  • 原文地址:https://www.cnblogs.com/wanfeng/p/3160006.html
Copyright © 2020-2023  润新知