Entity Framework 連表查詢 多樣寫法


使用EF實現多查詢,暫未考慮性能問題

2015年10月6日22:26:51

=====正文=====

我在數據庫有三個表,如下圖:

sysMenus與sysFunction有主外鍵關系,而sysUserInfo則與其他二表並無關聯。

 我現在使用這三個表針對多種情況使用EF實現多種查詢手段

方法一: include方法(或EF自身的導航屬性)

  查詢,要的就是出數據罷了。所以我先說EF自身的導航屬性,因為這也算常用方法。

  我所說的導航屬性就是:

模型表下方顯示的,可以看到只有擁有主外鍵關聯的表才會有導航屬性。

使用起來也特別簡單,上代碼

 

直接點出來就是表,真是不要太簡單了,但是這樣查詢是有缺點的,因為它不想include方法那樣全部查出來,是當你使用導航屬性點出字段是再去數據庫查一次,因此推薦使用include方法。

 

  而使用include方法則是:

除了多了一個include方法,其他一致的。

區別在於,sql語句生成的不同,使用sql檢測工具便知

 

SELECT TOP (1) 
    [Extent1].[fID] AS [fID], 
    [Extent1].[mID] AS [mID], 
    [Extent1].[fName] AS [fName], 
    [Extent1].[fFunction] AS [fFunction], 
    [Extent1].[fPicname] AS [fPicname], 
    [Extent1].[fStatus] AS [fStatus], 
    [Extent1].[fCreatorID] AS [fCreatorID], 
    [Extent1].[fCreateTime] AS [fCreateTime], 
    [Extent1].[fUpdateID] AS [fUpdateID], 
    [Extent1].[fUpdateTime] AS [fUpdateTime], 
    [Extent2].[mID] AS [mID1], 
    [Extent2].[mParentID] AS [mParentID], 
    [Extent2].[mName] AS [mName], 
    [Extent2].[mUrl] AS [mUrl], 
    [Extent2].[mArea] AS [mArea], 
    [Extent2].[mController] AS [mController], 
    [Extent2].[mAction] AS [mAction], 
    [Extent2].[mSortid] AS [mSortid], 
    [Extent2].[mStatus] AS [mStatus], 
    [Extent2].[mPicname] AS [mPicname], 
    [Extent2].[mLevel] AS [mLevel], 
    [Extent2].[mExp1] AS [mExp1], 
    [Extent2].[mExp2] AS [mExp2], 
    [Extent2].[mCreatorID] AS [mCreatorID], 
    [Extent2].[mCreateTime] AS [mCreateTime], 
    [Extent2].[mUpdateID] AS [mUpdateID], 
    [Extent2].[mUpdateTime] AS [mUpdateTime]
    FROM  [dbo].[sysFunction] AS [Extent1]
    INNER JOIN [dbo].[sysMenus] AS [Extent2] ON [Extent1].[mID] = [Extent2].[mID]

 

include方法會inner join 表查詢出所有數據,而直接使用導航屬性的僅僅只是單表查詢(沒有連sysMenus表怎么與mName呢?疑問點)

 所以我還是使用include

但是沒有主外鍵關系的表模型並沒有導航屬性,於是include並不能使用在表之間沒有主外鍵關系的表中!

 

方法二:使用join方法

有主外鍵關系

生成的sql語句

SELECT 
    [Extent1].[mID] AS [mID], 
    [Extent1].[mName] AS [mName], 
    [Extent2].[fName] AS [fName]
    FROM  [dbo].[sysMenus] AS [Extent1]
    INNER JOIN [dbo].[sysFunction] AS [Extent2] ON [Extent1].[mID] = [Extent2].[mID]

無主外鍵關系

sql語句

SELECT 
    [Extent1].[mID] AS [mID], 
    [Extent1].[mName] AS [mName], 
    [Extent2].[uLoginName] AS [uLoginName]
    FROM  [dbo].[sysMenus] AS [Extent1]
    INNER JOIN [dbo].[sysUserInfo] AS [Extent2] ON [Extent1].[mID] = [Extent2].[uID]

由生成的SQL語句可知,join也可完美連表,不管兩表是否有主外鍵關聯。但join方法有一缺點,無法使用多個join連表,要連三個表怎么做呢?答:join做不到啊

 

方法三:使用類SQL語句(如何讀?)

SELECT TOP (1) 
    [Extent1].[mID] AS [mID], 
    [Extent1].[mParentID] AS [mParentID], 
    [Extent1].[mName] AS [mName], 
    [Extent1].[mUrl] AS [mUrl], 
    [Extent1].[mArea] AS [mArea], 
    [Extent1].[mController] AS [mController], 
    [Extent1].[mAction] AS [mAction], 
    [Extent1].[mSortid] AS [mSortid], 
    [Extent1].[mStatus] AS [mStatus], 
    [Extent1].[mPicname] AS [mPicname], 
    [Extent1].[mLevel] AS [mLevel], 
    [Extent1].[mExp1] AS [mExp1], 
    [Extent1].[mExp2] AS [mExp2], 
    [Extent1].[mCreatorID] AS [mCreatorID], 
    [Extent1].[mCreateTime] AS [mCreateTime], 
    [Extent1].[mUpdateID] AS [mUpdateID], 
    [Extent1].[mUpdateTime] AS [mUpdateTime]
    FROM   [dbo].[sysMenus] AS [Extent1]
    INNER JOIN [dbo].[sysFunction] AS [Extent2] ON [Extent1].[mID] = [Extent2].[mID]
    INNER JOIN [dbo].[sysUserInfo] AS [Extent3] ON [Extent1].[mID] = [Extent3].[uID]

就像寫SQL語句一樣,但我感覺跟include是一樣的,因為沒有導航屬性,查出來也用不了

 

如有錯誤之處,望各位看管能不吝指出。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM