Castle ActiveRecord(二) 自动生成实体类
Castle.ActiveRecord.Generator可以让我们经过简单的配置,自动生成实体类,下面是Castle.ActiveRecord.Generator的界面:
切换到Project Explorer面板,点击添加数据库连接图标(位置如下图中2处所示),在弹出界面中填写数据库联接的别名和连接字符串,添加你需要的数据库联接。
添加完成之后,你就能看到数据库中包含的客户表及各表的字段了,如下图:
然后切换到ActiveRecord Components面板,然后选中ActiveRecord并拖动到左边的空白区域,会出现如下界面,此时选择我们刚才设置的数据库别名,然后下一步,选择你需要生成实体类的数据表,这里我们选择Bolg,然后下一步,选择需要映射的字段(列),然后下一步,因为blog表与post表有关联,所以会出现关联映射表(Relationships mapping)的选择,这个例子中我们先不选择与Blogs表关联的Posts表,然后下一步到有“Finsh”按钮的界面,给实体类起一个名称,点击finsh按钮,出现以下界面:
右键单击Blog,选择Preview code,就能看到生成的实体类代码了,点击Properties还能查看修改相关属性。
点击“Project”菜单,选择“Generate code”,弹出如下界面:
设置Namespace,选择采访路径及编程语言,然后点击“Generate”按钮,实体类就完全生成了,下面是我们生成的实体类Blog的代码,怎么样,够简单吧?
//
// Generated by ActiveRecord Generator
//
//
namespace Blog
{
using Castle.ActiveRecord;
[ActiveRecord("Blogs")]
public class Blog : ActiveRecordBase
{
private int _blogId;
private string _blogName;
private string _blogAuthor;
[PrimaryKey(PrimaryKeyType.Native, "blog_id")]
public int BlogId
{
get
{
return this._blogId;
}
set
{
this._blogId = value;
}
}
[Property(Column="blog_name")]
public string BlogName
{
get
{
return this._blogName;
}
set
{
this._blogName = value;
}
}
[Property(Column="blog_author")]
public string BlogAuthor
{
get
{
return this._blogAuthor;
}
set
{
this._blogAuthor = value;
}
}
public static void DeleteAll()
{
ActiveRecordBase.DeleteAll(typeof(Blog));
}
public static Blog[] FindAll()
{
return ((Blog[])(ActiveRecordBase.FindAll(typeof(Blog))));
}
public static Blog Find(int BlogId)
{
return ((Blog)(ActiveRecordBase.FindByPrimaryKey(typeof(Blog), BlogId)));
}
}
}
// Generated by ActiveRecord Generator
//
//
namespace Blog
{
using Castle.ActiveRecord;
[ActiveRecord("Blogs")]
public class Blog : ActiveRecordBase
{
private int _blogId;
private string _blogName;
private string _blogAuthor;
[PrimaryKey(PrimaryKeyType.Native, "blog_id")]
public int BlogId
{
get
{
return this._blogId;
}
set
{
this._blogId = value;
}
}
[Property(Column="blog_name")]
public string BlogName
{
get
{
return this._blogName;
}
set
{
this._blogName = value;
}
}
[Property(Column="blog_author")]
public string BlogAuthor
{
get
{
return this._blogAuthor;
}
set
{
this._blogAuthor = value;
}
}
public static void DeleteAll()
{
ActiveRecordBase.DeleteAll(typeof(Blog));
}
public static Blog[] FindAll()
{
return ((Blog[])(ActiveRecordBase.FindAll(typeof(Blog))));
}
public static Blog Find(int BlogId)
{
return ((Blog)(ActiveRecordBase.FindByPrimaryKey(typeof(Blog), BlogId)));
}
}
}
大家记得刚才我们生成实体类的时候,舍弃了关联映射,那么什么是关联映射呢,下一节我们将介绍。