castle ActiveRecord入门实例(1)-单表操作

1.下载软件

http://www.castleproject.org/activerecord/gettingstarted/index.html

http://www.castleproject.org/castle/download.html

 

2.配置文件appconfig.xml

 

<?xml version="1.0" encoding="utf-8" ?>
<activerecord>
  <config>
    <add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />
    <add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect" />
    <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
    <add key="connection.connection_string" value="Data Source=.;Initial Catalog=Movies;UID=sa;Password=123456" />
    <add key="proxyfactory.factory_class" value="NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu" />
    <add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" />
  </config>
</activerecord>

3.Global.asax

void Application_Start(object sender, EventArgs e)        

{

  XmlConfigurationSource source = new XmlConfigurationSource(Server.MapPath("~/appconfig.xml"));

  ActiveRecordStarter.Initialize(source, typeof(Movies));

}

4.实体类定义

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Castle.Core;
using Castle.ActiveRecord;
using System.Collections.Generic;
using System.Collections;


namespace WebApplication1
{
    [ActiveRecord("Movies")]
    public class Movies : ActiveRecordBase<Movies>
    {
        [PrimaryKey]
        public int ID{get;set;}
        [Property]
        public string Title{get;set;}
        [Property]
        public DateTime ReleaseDate{get;set;}
        [Property]
        public string Genre{get;set;}
        [Property]
        public double Price{get;set;}
        [Property]
        public string Rating{get;set;}

        public Movies()
        {
        }

         public static IList FindAll()
         {
             return (IList)FindAll(typeof(Movies));
         }

    }


}

 

5.数据操作

 

 //添加
 /**Movies m = new Movies();
m.Title = "m3";
m.ReleaseDate = DateTime.Now;
m.Price = 95;
m.Rating = "5";
m.Create();**/

//删除
/**
Movies m = Movies.Find(1);
m.Delete();
**/

//修改
/**Movies m = Movies.Find(2);
m.Title = "kenny";
m.Update();
**/

//查询
SimpleQuery<Movies> q1 = new SimpleQuery<Movies>(@"
from Movies m 
where m.Title =  ?
", "m2");
SimpleQuery<Movies> q2 = new SimpleQuery<Movies>(@"
from Movies m ");
Movies[] list = q2.Execute();
foreach (Movies mTmp in list)
{
  Response.Write("ID:" + mTmp.ID + ",Title:" + mTmp.Title + "<br/>");
}

 

 

 

 

你可能感兴趣的:(ActiveRecord)