deerchao的blog

Be and aware who you are.

Castle学习之二:处理主键时PrimaryKeyType.Assigned等几个有用的属性

ActiveRecord里主键是不应该被赋值的,调用ActiveRecordBase.Create()时,会自动生成一个新的主键.

如果有以下的代码:
 1     [ActiveRecord("book_School")]
 2     public class School : ActiveRecordBase<School>
 3     {
 4         Guid _id;
 5         string _name;
 6 
 7         [PrimaryKey(PrimaryKeyType.GuidComb]
 8         public Guid Id
 9         {
10             get { return _id; }
11             set { _id = value; }
12         }
13 
14         [Property(NotNull=true)]
15         public string Name
16         {
17             get { return _name; }
18             set { _name = value; }
19         }
20     }

我们在使用时,由于Id是可赋值的,很容易犯错,有没有办法让编译器保证我们没有对Id进行赋值呢?

答案是有:直接把属性Id的setter去掉就行了.

但是这样一来,ActiveRecord框架本身想要设置这个属性时就有麻烦了,它找不到setter,所以我们必须告诉它怎么设置这个属性的值:把PrimaryKeyAttribute的Access设置成PropertyAccess.FieldCamelcaseUnderscore(意思是给这个属性赋值时,如果找不到setter,就直接向名称符合以下规则的字段赋值,这个规则就是:把属性的名字转换成camelCase,然后再前面加一个下划线).这样一来,ActiveRecord向Id设置值时,发现这个属性没有setter,于是向名为_id的字段赋值.
 1     [ActiveRecord("book_School")]
 2     public class School : ActiveRecordBase<School>
 3     {
 4         Guid _id;
 5         string _name;
 6 
 7         [PrimaryKey(PrimaryKeyType.GuidComb, Access = PropertyAccess.FieldCamelcaseUnderscore)]
 8         public Guid Id
 9         {
10             get { return _id; }
11         }
12 
13         [Property(NotNull=true)]
14         public string Name
15         {
16             get { return _name; }
17             set { _name = value; }
18         }
19     }

考虑到大家的个人习惯不同,ActiveRecord还提供了FieldCamelcase (例:userId), FieldLowercaseUnderscore (例:_userid), FieldPascalcaseMUnderscore(例:m_UserId).

如果一定要手工设置主键的值,需要使用到PrimaryKeyType.Assigned.据NHibernate的文档所述:

[quote]

If you want the application to assign identifiers (as opposed to having NHibernate generate them), you may use the assigned generator. This special generator will use the identifier value already assigned to the object's identifier property. Be very careful when using this feature to assign keys with business meaning (almost always a terrible design decision).

Due to its inherent nature, entities that use this generator cannot be saved via the ISession's SaveOrUpdate() method. Instead you have to explicitly specify to NHibernate if the object should be saved or updated by calling either the Save() or Update() method of the ISession.

[/quote]

下面是综合运用以上所说的属性的代码:

School.cs

TClass.cs

这里,为了与asp.net MemberShip Provider对接, UserInfo.Id不可能由ActiveRecord自动生成,必须使用属性PrimaryKeyType.Assigned:
UserInfo.cs

Tests.cs

posted on 2007-02-25 21:53 deerchao 阅读(646) 评论(6)  编辑 收藏 网摘 所属分类: Castle

评论

#1楼  2007-03-22 10:00 清风 [未注册用户]

我有两个表,分别是Dept部门和Employee员工表
现运行以下代码出错:
[HasMany(typeof(Employee),
Table = "EmployeeInfo", ColumnKey = "DeptID",
Inverse = true , Cascade = ManyRelationCascadeEnum.AllDeleteOrphan)]
public IList<Employee> Employees
{
get { return _employees; }
set { _employees = value; }
}
错误描述为:
Could not guess relation type for property Dept.Employees

但是改为以下正确:
[HasMany(typeof(Employee),
Table = "EmployeeInfo", ColumnKey = "DeptID",
Inverse = true , Cascade = ManyRelationCascadeEnum.AllDeleteOrphan)]
public IList Employees
{
get { return _employees; }
set { _employees = value; }
}

是不是HasMany不支持泛型,要怎么改,谢谢!~
  回复  引用    

#2楼 [楼主] 2007-03-22 10:09 deerchao      

在Google里搜索"Could not guess relation type for property",第一条结果就是Castle的论坛里对这个问题的解答.   回复  引用  查看    

#3楼  2007-03-22 10:19 清风      

谢谢:)   回复  引用  查看    

#4楼  2007-04-03 16:08 will [未注册用户]

IList<Item> - may be <bag>, <list>, etc
Try specifying
[HasMany(RelationType=RelationType.Bag)]

And on general, go with Sets.   回复  引用    

#5楼  2007-04-03 16:21 will [未注册用户]

I tried to use

RelationType=RelationType.List

when i create schema , it raised error that
"
Index was outside the bounds of the array.

StackTrace:
at NHibernate.Mapping.Column.set_Name(String value)
"

so i changed to

RelationType=RelationType.Set

and it works fine.   回复  引用    

#6楼  2007-11-17 14:54 yuezhihun [未注册用户]

RelationType=RelationType.Set 在创建对象方面是解决了问题,但是在find上有错误,不知道该怎么解决   回复  引用    


标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2007-02-25 22:42 编辑过
Google站内搜索


China-pub 计算机图书网上专卖店!6.5万品种 2-8折!
近千种 9-95 新二手计算图书火热销售中!

相关文章:

相关链接:


 
<2008年11月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

导航

统计

公告

给网络添加价值,就是让自己增加价值.

本博客所有内容,均为原创或对互联网已有资源的再加工,希望对你有用.在声明原作者的前提下,你可以任意使用,但本人对其正确性,使用的后果等不做任何担保,也不负任何责任.

正则表达式30分钟入门教程 v2.21 2007-8-3

I Want Spec#!

与我联系

搜索

 

常用链接

留言簿(66)

我管理的小组

我的标签

随笔档案(127)

文章分类(9)

文章档案(9)

新闻档案(9)

Links

积分与排名

最新评论

评论排行榜