ActiveRecord 提供一个DomainModel 工具,它使我们创建对象模型的相互关系非常容易。在上面的例子中我们已经创建了一个Blog类,它完全可以提供一个Post类及它们之间的关联。平白点讲,他们的关联是:
- 一个Post属于一个Blog
- 一个Blog有多个Post
使用HasMany特性 和BelongsTo特性
必须使用HasMany和BelongTo特性表达对象之间的关联。如下:
[ActiveRecord(
"
Blogs
"
)]
public
class
Blog : ActiveRecordBase
{ private IList _posts;
[HasMany(typeof(Post), Table="posts", ColumnKey="post_blogid")]
public IList Posts
{
get
{ return _posts; }
set
{ _posts = value; }
}
}
[ActiveRecord(
"
Posts
"
)]
public
class
Post : ActiveRecordBase
{ private Blog _blog;
[BelongsTo("post_blogid")]
public Blog Blog
{
get
{ return _blog; }
set
{ _blog = value; }
}
}
在这个例子中,由于Post有一个BelongTo联合,ActiveRecord能够收集到这个信息并创建正确的HasMany,因此不必指定所有信息,象下面这样就可以了,注意对比“[HasMany(typeof(Post))]”:
[ActiveRecord(
"
Blogs
"
)]
public
class
Blog : ActiveRecordBase
{ private IList _posts;
[HasMany(typeof(Post))]
public IList Posts
{
get
{ return _posts; }
set
{ _posts = value; }
}
}
不要感到疑惑,让我们看看Post类,注意[BelongsTo("post_blogid")]:
using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
ActiveRecord
{ using Castle.ActiveRecord;
[ActiveRecord("Posts")]
public class Post : ActiveRecordBase
{
private int _postId;
private string _postTitle;
private string _postContents;
private string _postCategory;
private System.DateTime _postCreated;
private bool _postPublished;
private Blog _blog;
[PrimaryKey(PrimaryKeyType.Native, "post_id")]
public int PostId
{
get
{
return this._postId;
}
set
{
this._postId = value;
}
}
[Property(Column = "post_title")]
public string PostTitle
{
get
{
return this._postTitle;
}
set
{
this._postTitle = value;
}
}
[Property(Column = "post_contents")]
public string PostContents
{
get
{
return this._postContents;
}
set
{
this._postContents = value;
}
}
[Property(Column = "post_category")]
public string PostCategory
{
get
{
return this._postCategory;
}
set
{
this._postCategory = value;
}
}
[Property(Column = "post_created")]
public System.DateTime PostCreated
{
get
{
return this._postCreated;
}
set
{
this._postCreated = value;
}
}
[Property(Column = "post_published")]
public bool PostPublished
{
get
{
return this._postPublished;
}
set
{
this._postPublished = value;
}
}
[BelongsTo("post_blogid")]
public Blog Blog
{
get
{
return this._blog;
}
set
{
this._blog = value;
}
}
public static void DeleteAll()
{
ActiveRecordBase.DeleteAll(typeof(Post));
}
public static Post[] FindAll()
{
return ((Post[])(ActiveRecordBase.FindAll(typeof(Post))));
}
public static Post Find(int PostId)
{
return ((Post)(ActiveRecordBase.FindByPrimaryKey(typeof(Post), PostId)));
}
}
}
好,我们来看看怎么使用关联映射吧。先看下面的代码:
Blog blog
=
Blog.Find(
1
);newPost.PostTitle
=
"
MyPostTitle
"
;newPost.PostPublished
=
true
;newPost.PostContents
=
"
This is a great post!
"
;newPost.PostCategory
=
"
Blogging
"
;newPost.PostCreated
=
DateTime.Now;blog.Posts.Add(newPost );
blog.Save();
这段代码是无法正常运行的,因为Post类是一个瞬态类,在使用时我们必须先持久化它,象下面这样就可以运行了:
Blog blog
=
Blog.Find(
1
);Post newPost
=
new
Post();newPost.PostTitle
=
"
MyPostTitle
"
;newPost.PostPublished
=
true
;newPost.PostContents
=
"
This is a great post!
"
;newPost.PostCategory
=
"
Blogging
"
;newPost.PostCreated
=
DateTime.Now;newPost.Blog
=
blog;newPost.Save();
blog.Posts.Add(newPost);
blog.Save();
不过你的Blogs表中要有记录才行,否则会发生异常,告诉你找不到。又有人不禁想出了下面的代码:
Blog blog
=
New Blog();blog.BlogName
=
"
blogname
"
;blog.BlogAuthor
=
"
This is my first post
"
);Post newPost
=
new
Post();newPost.PostTitle
=
"
MyPostTitle
"
;newPost.PostPublished
=
true
;newPost.PostContents
=
"
This is a great post!
"
;newPost.PostCategory
=
"
Blogging
"
;newPost.PostCreated
=
DateTime.Now;newPost.Save();
blog.Posts.Add(newPost);
blog.Save();
这样还是不行地,在向数据库里面保存时,Post实际需要的是Blog的Id,而此时Blog还没有Id。
上面的代码都使用了Blog端的关联,也可以象下面这样使用:
Blog blog
=
Blog.Find(
1
);Post newPost
=
new
Post();newPost.PostTitle
=
"
MyPostTitle
"
;newPost.PostPublished
=
true
;newPost.PostContents
=
"
This is a great post!
"
;newPost.PostCategory
=
"
Blogging
"
;newPost.PostCreated
=
DateTime.Now;newPost.Blog
=
blog;newPost.Blog
=
blog;
//
Linking them
newPost.Save();
处理blog的时候,Posts不会自动地得到通知。如果想应用最新的改变,也好就是说要想使用新实例化的post,必须“刷新”它。我们可以通过设置级联变化来使用它:
[HasMany(
typeof
(Post), Table
=
"
Posts
"
, ColumnKey
=
"
post_blogid
"
, Cascade
=
ManyRelationCascadeEnum.SaveUpdate)]
public
IList Posts
{ get
{ return _posts; }
set
{ _posts = value; }
}
这样的开头那段代码就能运行了:
Blog blog
=
Blog.Find(
1
);newPost.PostTitle
=
"
MyPostTitle
"
;newPost.PostPublished
=
true
;newPost.PostContents
=
"
This is a great post!
"
;newPost.PostCategory
=
"
Blogging
"
;newPost.PostCreated
=
DateTime.Now;blog.Posts.Add(newPost );
blog.Save();
怎么样,一定有点晕了吧,不要紧,下一节我们将讲解一下HasManyAttribute和BelongsToAttribute。
转载于:https://www.cnblogs.com/ttinfo/archive/2006/07/27/461180.html
原文链接:https://blog.csdn.net/weixin_30342827/article/details/94890497
本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。
还没有人抢沙发呢~