好了,現在我們就可以在查詢句法中直接關聯表了(資料庫中不一定要設定表的外來鍵關
系):
response.write(
"-------------
查詢分類為
1 的版塊
-------------");
varquery1 =
from
b in
ctx.boards
where
b.category.categoryid == 1
selectb;
foreach
(board
b in
query1)
response.write(b.boardid +
" "+ b.boardname + ""
); response.write(
"-------------
查詢版塊大於
2 個的分類
-------------");
varquery2 =
from
c in
ctx.boardcategories
where
c.boards.count > 2
select
c;
foreach
(boardcategory
c in
query2)
response.write(c.categoryid +
" "+ c.categoryname+
" "+
c.boards.count + ""
); 在資料庫中加一些測試資料,
便我不需要再用語言形容了吧。執行上述的程式會導致下面
sql
的執行:
select [t0].[boardid], [t0].[boardname],[t0].[boardcategory]
from [boards] as [t0]
inner join [categories] as [t1] on[t1].[categoryid] = [t0].[boardcategory]
where [t1].[categoryid] = @p0
-- @p0: input int32 (size = 0; prec = 0;scale = 0) [1]
select [t0].[categoryid],[t0].[categoryname]
from [categories] as [t0]
where ((
select count(*)
from [boards] as [t1]
where [t1].[boardcategory] = [t0].[categoryid]
)) > @p0
-- @p0: input int32 (size = 0; prec = 0; scale= 0) [2]
select [t0].[boardid], [t0].[boardname],[t0].[boardcategory]
from [boards] as [t0]
where [t0].[boardcategory] = @p0
-- @p0: input int32 (size = 0; prec = 0;scale = 0) [1]
可以看到,第二個查詢並沒有做外連線,還記得
dataloadoptions
嗎?我們可以要求
linq to sql
在讀取版塊分類資訊的時候也把版塊資訊一起載入:
dataloadoptions
options =
new
dataloadoptions
();
options.loadwith<
boardcategory
>(c => c.boards);
ctx.loadoptions = options;
response.write(
"-------------
查詢版塊大於
2 個的分類
-------------");
varquery2 =
from
c in
ctx.boardcategories
where
c.boards.count > 2
select
c;
foreach
(boardcategory
c in
query2)
response.write(c.categoryid +
" "+ c.categoryname+
" "+
c.boards.count + ""
); 查詢經過改造後會得到下面的
sql:
select [t0].[categoryid],[t0].[categoryname], [t1].[boardid], [t1].[boardname],
[t1].[boardcategory], (
select count(*)
from [boards] as [t3]
where [t3].[boardcategory] = [t0].[categoryid]
)as [count]
from [categories] as [t0]
left outer join [boards] as [t1] on[t1].[boardcategory] = [t0].[categoryid]
where ((
select count(*)
from [boards] as [t2]
where [t2].[boardcategory] = [t0].[categoryid]
)) > @p0
order by [t0].[categoryid], [t1].[boardid]
-- @p0: input int32 (size = 0; prec = 0;scale = 0) [2]
在新增分類的時候,如果這個分類下還有新的版塊,那麼提交新增分類的時候版塊也
會新增:
boardcategory
dbcat =
new
boardcategory
() ;
board
oracle =
new
board
() ;
ctx.boardcategories.add(dbcat);
ctx.submitchanges();
上述**導致下面的
sql
被執行:
insert into [categories]([categoryname])values (@p0)
select [t0].[categoryid]
from [categories] as [t0]
where [t0].[categoryid] =(scope_identity())
-- @p0: input ansistring (size = 8; prec =0; scale = 0) [database]
insert into [boards]([boardname],[boardcategory]) values (@p0, @p1)
select [t0].[boardid]
from [boards] as [t0]
where [t0].[boardid] = (scope_identity())
-- @p0: input ansistring (size = 6; prec =0; scale = 0) [oracle]
-- @p1: input int32 (size = 0; prec = 0;scale = 0) [23]
mysql實體 mysql實體關係
實體關係 表設計1 1 兩個實體表內,存在相同的主鍵字段 如果記錄的主鍵值等於另乙個關係表內記錄的主鍵值,則兩條記錄的對應為一一對應 優化上稱為垂直分割 1 n乙個實體對應多個其他實體 乙個班級對應多個學生 設計 在多的那段增加乙個字段,用於指向該實體所屬的另外實體的標識 m n多對多 設計典型的利...
對映實體關聯關係
乙個類中有兩個一對一關聯。class a class ba表 idname home id 指向b表中的id,外來鍵 company id 指向b表中的id,外來鍵 b表address street ida對映檔案 cascsde all b對映檔案 只有乙個一對一關聯。class a class ...
JPA的實體關係對映總結
jpa是ejb3.0中引入的實體 關係持久化模型。通常,每個實體類相當於是資料庫中的一張表,而每個物件則是表中的一條記錄。我們知道,資料庫中的表之間會存在一定的依賴,這可以通過 主外來鍵 來體現出來。物件關係對映提供了一種將物件對映到資料庫的機制 orm 其中jpa就是這樣的一種標準。有了orm,我...