实体关系的定义

实体关系的定义
 
比如我们的论坛分类表和论坛版块表之间就有关系,这种关系是 1 对多的关系。也就


是说一个论坛分类可能有多个论坛版块,这是很常见的。定义实体关系的优势在于,我们无


须显式作连接操作就能处理关系表的条件。


首先来看看分类表的定义:

[Table(Name = "Categories")]


public class BoardCategory


{


        [Column(Name = "CategoryID", DbType = "int identity", IsPrimaryKey = true,


IsDbGenerated = true, CanBeNull = false)]


public int CategoryID { get; set; } 
   [Column(Name = "CategoryName", DbType = "varchar(50)", CanBeNull = false)]


public string CategoryName { get; set; }

private EntitySet<Board> _Boards;

    [Association(OtherKey = "BoardCategory", Storage = "_Boards")]

public EntitySet<Board> Boards


        { 


get { return this._Boards; }


set { this._Boards.Assign(value); }


        } 

public BoardCategory()


        { 


this._Boards = new EntitySet<Board>();


        } 


}
 
CategoryID 和 CategoryName 的映射没有什么不同,只是我们还增加了一个 Boards


属性,它返回的是 Board 实体集。通过特性,我们定义了关系外键为 BoardCategory(Board


表的一个字段)。然后来看看 1 对多,多端版块表的实体:

[Table(Name = "Boards")]


public class Board


{


        [Column(Name = "BoardID", DbType = "int identity", IsPrimaryKey = true,


IsDbGenerated = true, CanBeNull = false)]


public int BoardID { get; set; }

        [Column(Name = "BoardName", DbType = "varchar(50)", CanBeNull = false)]


public string BoardName { get; set; }
[Column(Name = "BoardCategory", DbType = "int", CanBeNull = false)]


public int BoardCategory { get; set; }

private EntityRef<BoardCategory> _Category;
     [Association(ThisKey = "BoardCategory", Storage = "_Category")]


public BoardCategory Category


        { 


get { return this._Category.Entity; }


set


                {   


this._Category.Entity = value;


value.Boards.Add(this);


                } 


}


}


在这里我们需要关联分类,设置了 Category 属性使用 BoardCategory 字段和分类表关


联。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值