知识点随笔记录二

十二、linq关联表查询自己总结的几种方式:
1、直接join(可以不使用导航属性
2、var user1 = from b in db.sysUsers
               from c in b.SysUserRoles
               where b.ID == id
               select b;
必须使用导航属性
生成的SQL:
SELECT TOP (1)
[Extent1].[ID] AS [ID],
[Extent1].[UserName] AS [UserName],
[Extent1].[Email] AS [Email],
[Extent1].[PassWord] AS [PassWord],
[Extent1].[CreateDate] AS [CreateDate],
[Extent1].[SysDepartmentID] AS [SysDepartmentID]
FROM  [dbo].[SysUser] AS [Extent1]
INNER JOIN [dbo].[SysUserRole] AS [Extent2] ON [Extent1].[ID] = [Extent2].[SysUserID]
WHERE [Extent1].[ID] = 5

可以知道,是INNER JOIN。

3、Include方式(Eager loading)
var user = from a in db.sysUsers.Include("SysUserRoles").Include("SysDepartment")
           where a.ID == id
           select a;
必须使用导航属性
生成的SQL:
SELECT
[Project1].[ID] AS [ID],
[Project1].[UserName] AS [UserName],
[Project1].[Email] AS [Email],
[Project1].[PassWord] AS [PassWord],
[Project1].[CreateDate] AS [CreateDate],
[Project1].[SysDepartmentID] AS [SysDepartmentID],
[Project1].[ID1] AS [ID1],
[Project1].[DepartmentName] AS [DepartmentName],
[Project1].[DepartmentDesc] AS [DepartmentDesc],
[Project1].[C1] AS [C1],
[Project1].[ID2] AS [ID2],
[Project1].[SysUserID] AS [SysUserID],
[Project1].[SysRoleID] AS [SysRoleID]
FROM ( SELECT
    [Limit1].[ID] AS [ID],
    [Limit1].[UserName] AS [UserName],
    [Limit1].[Email] AS [Email],
    [Limit1].[PassWord] AS [PassWord],
    [Limit1].[CreateDate] AS [CreateDate],
    [Limit1].[SysDepartmentID] AS [SysDepartmentID],
    [Extent2].[ID] AS [ID1],
    [Extent2].[DepartmentName] AS [DepartmentName],
    [Extent2].[DepartmentDesc] AS [DepartmentDesc],
    [Extent3].[ID] AS [ID2],
    [Extent3].[SysUserID] AS [SysUserID],
    [Extent3].[SysRoleID] AS [SysRoleID],
    CASE WHEN ([Extent3].[ID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
    FROM    (SELECT TOP (1) [Extent1].[ID] AS [ID], [Extent1].[UserName] AS [UserName], [Extent1].[Email] AS [Email], [Extent1].[PassWord] AS [PassWord], [Extent1].[CreateDate] AS [CreateDate], [Extent1].[SysDepartmentID] AS [SysDepartmentID]
        FROM [dbo].[SysUser] AS [Extent1]
        WHERE [Extent1].[ID] = 4 ) AS [Limit1]
    LEFT OUTER JOIN [dbo].[SysDepartment] AS [Extent2] ON [Limit1].[SysDepartmentID] = [Extent2].[ID]
    LEFT OUTER JOIN [dbo].[SysUserRole] AS [Extent3] ON [Limit1].[ID] = [Extent3].[SysUserID]
)  AS [Project1]
ORDER BY [Project1].[ID] ASC, [Project1].[ID1] ASC, [Project1].[C1] ASC
               
是LEFT OUTER JOIN。

十三、辅助器
1,内联辅助器,定义在视图里没有返回值的方法,只在该页面有效。以@helper定义。
2,外部辅助器,定义mvchtmlstring对象的扩展方法,内部定义html元素用TagBuilder类。
3,内建辅助器,包括表单辅助器、普通input辅助器、强类型input辅助器、普通select辅助器、强类型select辅助器、生成链接的辅助器等。   
   
十四、分段
1、在视图中使用@section定义分段内容,在布局中通过@RenderSection(分段名称)使用。
2、通用的部分可以定义成分段,如header、footer,甚至body也可以,布局中使用@RenderSection("header")、@RenderSection("footer")、@RenderSection("body")来使用分段;
3、IsSectionDefined(分段名称)检测分段是否已定义;
4、通过@RenderSection(分段名称,false)重载参数指定分段是否必须定义,如未定义也不会抛出异常。

十五、子动作
ChildActionOnly对于子动作不是必须的,加上是为了防止用户直接调用;

十六、模板视图辅助器(Templated View Helper)

不指定用什么HTML元素显示类型的属性,而是让MVC框架自己去判断该用什么样的HTML元素。
1、显示视图(如Display、DisplayFor)和编辑视图(如Edit、EditFor)
2、Display、DisplayFor、Edit、EditFor、Lable、LableFor、DisplayText、DisplayTextFor

3、支架(Scaffolding)
是为一个模型对象(而非针对单个属性)的所有属性生成HTML辅助器的套件。
DisplayForModel()
EditForModel()
LableForModel()

4、模板辅助器HTML辅助器的区分(自己总结)
模板辅助器不明确指定用哪个HTML元素显示,让MVC自己去判断;
HTML辅助器包括表单辅助器、Input辅助器、select辅助器、生成url辅助器等,是明确指定了用什么HTML控件来生成;
模板辅助器更加灵活;

5、总结:
模板(视图)辅助器有两大类,一类叫做模板辅助器,其作用就是在视图中为模型的个别属性创建显示/编辑视图;
另一类叫做支架模板视图,其作用是在视图中创建模型的所有属性的显示/编辑视图。

posted @ 2018-10-08 14:01  skybirdzw  阅读(114)  评论(0编辑  收藏  举报