NBear学习之路1:利用视图创建多表关联的实体

若还不清楚NBear中如何实现ORM,请先阅读Teddy所写的《 NBearV3 Step by Step教程——ORM篇 》。

通常在一些应用中所使用到的字段不仅仅来左一个数据表,如一个文章列表GirdView中,使用到了Content、User、Category三个表中的项。
关系图如下:

ContractedBlock.gif ExpandedBlockStart.gif Content.cs
  1None.gif    public interface Content : Entity
  2ExpandedBlockStart.gifContractedBlock.gif    dot.gif{
  3InBlock.gif        [PrimaryKey]
  4InBlock.gif        int ID
  5ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
  6InBlock.gif            get;
  7ExpandedSubBlockEnd.gif        }

  8InBlock.gif
  9InBlock.gif        [ManyToManyQuery(typeof(ContentTag), LazyLoad = true)]
 10InBlock.gif        Tag[] Tags
 11ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gifget;set; }
 12InBlock.gif
 13InBlock.gif        [FkReverseQuery(LazyLoad = false)]
 14InBlock.gif        Category Category
 15ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 16InBlock.gif            get;
 17InBlock.gif            set;
 18ExpandedSubBlockEnd.gif        }

 19InBlock.gif
 20InBlock.gif        DateTime? ModifiedDate
 21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 22InBlock.gif            get;
 23InBlock.gif            set;
 24ExpandedSubBlockEnd.gif        }

 25InBlock.gif
 26InBlock.gif        [FkReverseQuery(LazyLoad = false)]
 27InBlock.gif        [MappingName("UserID")]
 28InBlock.gif        User User
 29ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 30InBlock.gif            get;
 31InBlock.gif            set;
 32ExpandedSubBlockEnd.gif        }

 33InBlock.gif
 34InBlock.gif        [SqlType("nvarchar(100)")]
 35InBlock.gif        string Title
 36ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 37InBlock.gif            get;
 38InBlock.gif            set;
 39ExpandedSubBlockEnd.gif        }

 40InBlock.gif
 41InBlock.gif        [SqlType("ntext")]
 42InBlock.gif        string Body
 43ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 44InBlock.gif            get;
 45InBlock.gif            set;
 46ExpandedSubBlockEnd.gif        }

 47InBlock.gif
 48InBlock.gif        [SqlType("ntext")]
 49ExpandedSubBlockStart.gifContractedSubBlock.gif        string Summary dot.gifget;set;}
 50InBlock.gif
 51InBlock.gif        [SqlType("nvarchar(250)")]
 52InBlock.gif        string Link
 53ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 54InBlock.gif            get;
 55InBlock.gif            set;
 56ExpandedSubBlockEnd.gif        }

 57InBlock.gif
 58InBlock.gif        int TopSort
 59ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 60InBlock.gif            get;
 61InBlock.gif            set;
 62ExpandedSubBlockEnd.gif        }

 63InBlock.gif
 64InBlock.gif        bool Visible
 65ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 66InBlock.gif            get;
 67InBlock.gif            set;
 68ExpandedSubBlockEnd.gif        }

 69InBlock.gif
 70InBlock.gif        int Hits
 71ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 72InBlock.gif            get;
 73InBlock.gif            set;
 74ExpandedSubBlockEnd.gif        }

 75InBlock.gif
 76InBlock.gif        [SqlType("nvarchar(50)")]
 77InBlock.gif        string IP
 78ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 79InBlock.gif            get;
 80InBlock.gif            set;
 81ExpandedSubBlockEnd.gif        }

 82InBlock.gif
 83InBlock.gif        bool AllowComment
 84ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 85InBlock.gif            get;
 86InBlock.gif            set;
 87ExpandedSubBlockEnd.gif        }

 88InBlock.gif
 89InBlock.gif        int CommentCount
 90ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 91InBlock.gif            get;
 92InBlock.gif            set;
 93ExpandedSubBlockEnd.gif        }

 94InBlock.gif
 95InBlock.gif        [FkQuery("Content", OrderBy = "{ID} DESC", Contained = true, LazyLoad = true)]
 96InBlock.gif        Comment[] Comments
 97ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 98InBlock.gif            get;
 99InBlock.gif            set;
100ExpandedSubBlockEnd.gif        }

101ExpandedBlockEnd.gif    }

ContractedBlock.gif ExpandedBlockStart.gif User.cs
  1None.gifpublic interface User : Entity
  2ExpandedBlockStart.gifContractedBlock.gif    dot.gif{
  3InBlock.gif        [PrimaryKey]
  4InBlock.gif        int ID
  5ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
  6InBlock.gif            get;
  7ExpandedSubBlockEnd.gif        }

  8InBlock.gif
  9InBlock.gif        [SqlType("nvarchar(50)")]
 10InBlock.gif        string Name
 11ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 12InBlock.gif            get;
 13InBlock.gif            set;
 14ExpandedSubBlockEnd.gif        }

 15InBlock.gif
 16InBlock.gif        [SqlType("nvarchar(50)")]
 17InBlock.gif        string Email
 18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 19InBlock.gif            get;
 20InBlock.gif            set;
 21ExpandedSubBlockEnd.gif        }

 22InBlock.gif
 23InBlock.gif        [FkQuery("UserID", Contained = true, LazyLoad = true)]
 24InBlock.gif        UserProfile Profile
 25ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 26InBlock.gif            get;
 27InBlock.gif            set;
 28ExpandedSubBlockEnd.gif        }

 29InBlock.gif
 30InBlock.gif        [ManyToManyQuery(typeof(UserGroup), OrderBy = "{Name} DESC", LazyLoad = true)]
 31InBlock.gif        [SerializationIgnore]
 32InBlock.gif        Group[] Groups
 33ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 34InBlock.gif            get;
 35InBlock.gif            set;
 36ExpandedSubBlockEnd.gif        }

 37InBlock.gif
 38InBlock.gif        [ManyToManyQuery(typeof(UserRole), OrderBy = "{Name} DESC", LazyLoad = true)]
 39InBlock.gif        [SerializationIgnore]
 40InBlock.gif        Role[] Roles
 41ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 42InBlock.gif            get;
 43InBlock.gif            set;
 44ExpandedSubBlockEnd.gif        }

 45InBlock.gif
 46InBlock.gif        [FkQuery("UserID", OrderBy = "{Name} DESC", Contained = true, LazyLoad = true)]
 47InBlock.gif        [SerializationIgnore]
 48InBlock.gif        Tag[] Tags
 49ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 50InBlock.gif            get;
 51InBlock.gif            set;
 52ExpandedSubBlockEnd.gif        }

 53InBlock.gif
 54InBlock.gif        [FkQuery("User", OrderBy = "{ID} DESC", Contained = true, LazyLoad = true)]
 55InBlock.gif        [SerializationIgnore]
 56InBlock.gif        Content[] Contents
 57ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 58InBlock.gif            get;
 59InBlock.gif            set;
 60ExpandedSubBlockEnd.gif        }

 61InBlock.gif
 62InBlock.gif        [FkQuery("User", OrderBy = "{ID} DESC", Contained = true, LazyLoad = true)]
 63InBlock.gif        [SerializationIgnore]
 64InBlock.gif        Comment[] Comments
 65ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 66InBlock.gif            get;
 67InBlock.gif            set;
 68ExpandedSubBlockEnd.gif        }

 69InBlock.gif
 70InBlock.gif        [FkQuery("User", OrderBy = "{Name} DESC", Contained = true, LazyLoad = true)]
 71InBlock.gif        [SerializationIgnore]
 72InBlock.gif        Category[] Categories
 73ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 74InBlock.gif            get;
 75InBlock.gif            set;
 76ExpandedSubBlockEnd.gif        }

 77InBlock.gif
 78InBlock.gif        UserStatus Status
 79ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 80InBlock.gif            get;
 81InBlock.gif            set;
 82ExpandedSubBlockEnd.gif        }

 83InBlock.gif
 84InBlock.gif        [SqlType("nvarchar(50)")]
 85InBlock.gif        string LogOnName
 86ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 87InBlock.gif            get;
 88InBlock.gif            set;
 89ExpandedSubBlockEnd.gif        }

 90InBlock.gif
 91InBlock.gif        [SqlType("nvarchar(50)")]
 92InBlock.gif        string Password
 93ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 94InBlock.gif            get;
 95InBlock.gif            set;
 96ExpandedSubBlockEnd.gif        }

 97InBlock.gif
 98InBlock.gif        [SqlType("nvarchar(100)")]
 99InBlock.gif        string PassQuestion
100ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
101InBlock.gif            get;
102InBlock.gif            set;
103ExpandedSubBlockEnd.gif        }

104InBlock.gif
105InBlock.gif        [SqlType("nvarchar(100)")]
106InBlock.gif        string PassAnswer
107ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
108InBlock.gif            get;
109InBlock.gif            set;
110ExpandedSubBlockEnd.gif        }

111InBlock.gif
112InBlock.gif        [FkQuery("UserID", Contained = true, LazyLoad = true)]
113InBlock.gif        UserPhone[] Phones
114ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
115InBlock.gif            get;
116InBlock.gif            set;
117ExpandedSubBlockEnd.gif        }

118InBlock.gif
119InBlock.gif        [SqlType("nvarchar(30)")]
120InBlock.gif        string No
121ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
122InBlock.gif            get;
123InBlock.gif            set;
124ExpandedSubBlockEnd.gif        }

125InBlock.gif
126InBlock.gif        [SqlType("nvarchar(4)")]
127InBlock.gif        string Sex
128ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
129InBlock.gif            get;
130InBlock.gif            set;
131ExpandedSubBlockEnd.gif        }

132InBlock.gif
133InBlock.gif        DateTime? Birthday
134ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
135InBlock.gif            get;
136InBlock.gif            set;
137ExpandedSubBlockEnd.gif        }

138InBlock.gif
139InBlock.gif        DateTime? CreateDate
140ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
141InBlock.gif            get;
142InBlock.gif            set;
143ExpandedSubBlockEnd.gif        }

144InBlock.gif
145InBlock.gif        DateTime? LogOnDate
146ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
147InBlock.gif            get;
148InBlock.gif            set;
149ExpandedSubBlockEnd.gif        }

150InBlock.gif
151InBlock.gif        int LogOnCount
152ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
153InBlock.gif            get;
154InBlock.gif            set;
155ExpandedSubBlockEnd.gif        }

156ExpandedBlockEnd.gif    }

ContractedBlock.gif ExpandedBlockStart.gif Category.cs
 1None.gifpublic interface Category : Entity
 2ExpandedBlockStart.gifContractedBlock.gif    dot.gif{
 3InBlock.gif        [PrimaryKey]
 4InBlock.gif        int ID
 5ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 6InBlock.gif            get;
 7ExpandedSubBlockEnd.gif        }

 8InBlock.gif
 9InBlock.gif        [SqlType("nvarchar(50)")]
10InBlock.gif        string Name
11ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
12InBlock.gif            get;
13InBlock.gif            set;
14ExpandedSubBlockEnd.gif        }

15InBlock.gif
16InBlock.gif        [SqlType("nvarchar(250)")]
17InBlock.gif        string Description
18ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
19InBlock.gif            get;
20InBlock.gif            set;
21ExpandedSubBlockEnd.gif        }

22InBlock.gif
23InBlock.gif        [FkQuery("ParentID", Contained = true, LazyLoad = true)]
24ExpandedSubBlockStart.gifContractedSubBlock.gif        Setting Setting dot.gifget;set; }
25InBlock.gif
26InBlock.gif        [FkReverseQuery(LazyLoad = true)]
27InBlock.gif        [MappingName("ParentID")]
28InBlock.gif        [SerializationIgnore]
29InBlock.gif        Category Parent
30ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
31InBlock.gif            get;
32InBlock.gif            set;
33ExpandedSubBlockEnd.gif        }

34InBlock.gif
35InBlock.gif        [FkQuery("Parent", OrderBy = "{Name} DESC", LazyLoad = true)]
36InBlock.gif        Category[] Childs
37ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
38InBlock.gif            get;
39InBlock.gif            set;
40ExpandedSubBlockEnd.gif        }

41InBlock.gif
42InBlock.gif        [FkQuery("Category", OrderBy = "{ID} DESC", Contained = true, LazyLoad = true)]
43InBlock.gif        [SerializationIgnore]
44InBlock.gif        Content[] Contents
45ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
46InBlock.gif            get;
47InBlock.gif            set;
48ExpandedSubBlockEnd.gif        }

49InBlock.gif
50InBlock.gif        int Sort
51ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
52InBlock.gif            get;
53InBlock.gif            set;
54ExpandedSubBlockEnd.gif        }

55InBlock.gif
56InBlock.gif        [FkReverseQuery(LazyLoad = true)]
57InBlock.gif        User User
58ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
59InBlock.gif            get;
60InBlock.gif            set;
61ExpandedSubBlockEnd.gif        }

62InBlock.gif
63InBlock.gif        [FkReverseQuery(LazyLoad = true)]
64InBlock.gif        Group Group
65ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
66InBlock.gif            get;
67InBlock.gif            set;
68ExpandedSubBlockEnd.gif        }

69InBlock.gif
70InBlock.gif        bool Visible
71ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
72InBlock.gif            get;
73InBlock.gif            set;
74ExpandedSubBlockEnd.gif        }

75ExpandedBlockEnd.gif    }


而每个表对应的实体是通过NBear创建的,如果在绑定GirdView时,使用Content[]来做DataSource,再通过强类型来获得Content.User和Content.Category,这样每次都会进行两次查询,如果绑定的列有N条,再绑定一个GirdView的总查询次数将为2N+1。
例如:
1  this .GridView1.DataSource  =  gateway.GetPageSelector < Content > (Content._.CategoryID  ==  categoryID, orderBy, pageSize).FindPage(pageNo);
2  this .GridView1.DataBind();
在GirdView1中设置编定列,譬如Content.User.Name:
1 ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif # StrongTyped<Entities.User>(Container.DataItem).User.Name  %>
会发现,每次绑定一行时都会进行一次Select查询。分页大小越大,查询的次数则越多。

而通常我们需要使用多数据表中的数据项时,通常都是使用视图的,而NBear强大的支持从视图生成实体的功能,这样,可以轻而易举地解决这类问题,并且将查询次数减到最少。

1.首先,先建立关联三个表项的视图。结合上面的实体定义的SQL视图脚本如下:
ContractedBlock.gif ExpandedBlockStart.gif SQL视图脚本
 1None.gifif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[v_Content]'and OBJECTPROPERTY(id, N'IsView'= 1)
 2None.gifdrop view [dbo].[v_Content]
 3None.gifGO
 4None.gif
 5None.gifCREATE VIEW [v_Content]
 6None.gifAS
 7None.gifSELECT [Category].[Name] AS [CategoryName][Category].[Sort] AS [CategorySort],
 8None.gif[Category].[Visible] AS [CategoryVisible][Category].[Description] AS [CategoryDescription]
 9None.gif[Category].[ParentID] AS [CategoryParentID],
10None.gif[Category].[User_ID] AS [CategoryUserID][Category].[Group_ID] AS [CategoryGroupID],
11None.gif[Content].[ID][Content].[Category_ID][Content].[ModifiedDate]
12None.gif[Content].[UserID][Content].[Title][Content].[Body][Content].[Summary],
13None.gif[Content].[Link][Content].[TopSort][Content].[Visible]
14None.gif[Content].[Hits][Content].[IP][Content].[AllowComment][Content].[CommentCount],
15None.gif[User].[Name] AS [UserName][User].[Email] AS [UserEmail][User].[Status] AS [UserStatus],
16None.gif[User].[LogOnName] AS [UserLogOnName]
17None.gifFROM [Content] INNER JOIN [Category] ON [Category].[ID] = [Content].[Category_ID]
18None.gifINNER JOIN [User] ON [User].[ID] = [Content].[UserID]
19None.gifGO

2.使用NBear.Tools.DbToEntityDesign.exe工具从数据库中生成实体定义代码:
ContractedBlock.gif ExpandedBlockStart.gif ContentView.cs
 1None.gif[ReadOnly]
 2None.gif    [MappingName("v_Content")]
 3None.gif    public interface ContentView : Entity
 4ExpandedBlockStart.gifContractedBlock.gif    dot.gif{
 5InBlock.gif        [SqlType("nvarchar(50)")]
 6ExpandedSubBlockStart.gifContractedSubBlock.gif        string CategoryName dot.gifget; }
 7InBlock.gif
 8ExpandedSubBlockStart.gifContractedSubBlock.gif        int CategorySort dot.gifget; }
 9InBlock.gif
10ExpandedSubBlockStart.gifContractedSubBlock.gif        bool CategoryVisible dot.gifget; }
11InBlock.gif
12InBlock.gif        [SqlType("nvarchar(250)")]
13ExpandedSubBlockStart.gifContractedSubBlock.gif        string CategoryDescription dot.gifget; }
14InBlock.gif
15ExpandedSubBlockStart.gifContractedSubBlock.gif        int CategoryParentID dot.gifget; }
16InBlock.gif
17ExpandedSubBlockStart.gifContractedSubBlock.gif        int CategoryUserID dot.gifget; }
18InBlock.gif
19ExpandedSubBlockStart.gifContractedSubBlock.gif        int CategoryGroupID dot.gifget; }
20InBlock.gif
21ExpandedSubBlockStart.gifContractedSubBlock.gif        int ID dot.gifget; }
22InBlock.gif
23InBlock.gif        [MappingName("Category_ID")]
24ExpandedSubBlockStart.gifContractedSubBlock.gif        int CategoryID dot.gifget; }
25InBlock.gif
26ExpandedSubBlockStart.gifContractedSubBlock.gif        DateTime ModifiedDate dot.gifget; }
27InBlock.gif
28ExpandedSubBlockStart.gifContractedSubBlock.gif        int UserID dot.gifget; }
29InBlock.gif
30InBlock.gif        [SqlType("nvarchar(100)")]
31ExpandedSubBlockStart.gifContractedSubBlock.gif        string Title dot.gifget; }
32InBlock.gif
33InBlock.gif        [SqlType("ntext")]
34ExpandedSubBlockStart.gifContractedSubBlock.gif        string Summary dot.gifget; }
35InBlock.gif
36InBlock.gif        [SqlType("nvarchar(250)")]
37ExpandedSubBlockStart.gifContractedSubBlock.gif        string Link dot.gifget; }
38InBlock.gif
39ExpandedSubBlockStart.gifContractedSubBlock.gif        int TopSort dot.gifget; }
40InBlock.gif
41ExpandedSubBlockStart.gifContractedSubBlock.gif        bool Visible dot.gifget; }
42InBlock.gif
43ExpandedSubBlockStart.gifContractedSubBlock.gif        int Hits dot.gifget; }
44InBlock.gif
45InBlock.gif        [SqlType("nvarchar(50)")]
46ExpandedSubBlockStart.gifContractedSubBlock.gif        string IP dot.gifget; }
47InBlock.gif
48ExpandedSubBlockStart.gifContractedSubBlock.gif        bool AllowComment dot.gifget; }
49InBlock.gif
50ExpandedSubBlockStart.gifContractedSubBlock.gif        int CommentCount dot.gifget; }
51InBlock.gif
52InBlock.gif        [SqlType("ntext")]
53ExpandedSubBlockStart.gifContractedSubBlock.gif        string UserName dot.gifget; }
54InBlock.gif
55InBlock.gif        [SqlType("nvarchar(50)")]
56ExpandedSubBlockStart.gifContractedSubBlock.gif        string UserEmail dot.gifget; }
57InBlock.gif
58ExpandedSubBlockStart.gifContractedSubBlock.gif        int UserStatus dot.gifget; }
59InBlock.gif
60InBlock.gif        [SqlType("nvarchar(50)")]
61ExpandedSubBlockStart.gifContractedSubBlock.gif        string UserLogOnName dot.gifget; }
62ExpandedBlockEnd.gif    }

3.使用ContentView[]绑定到GirdView即可一次获取所要的结果。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值