10-9. 在多對多關係中爲插入和刪除使用存儲過程sql
問題數據庫
想要在一個無載荷的多對多關係中使用存儲過程(存儲過程隻影響關係的鏈接表)app
解決方案編輯器
假設有一個多對多關係的做者( Author)表和書籍( Book)表. 用鏈接表AuthorBook來作多對多關係,如 Figure 10-11.所示:ide
Figure 10-11. A payload-free, many-to-many relationship between an Author and a Bookspa
當把表生成模型,那麼模型就如Figure 10-12所示:設計
Figure 10-12. The model created by importing the tables in Figure 10-11blog
接下來用存儲過程建立插入和刪除操做:ip
1.在數據庫中,建立如Listing 10-23所示的存儲過程.ci
Listing 10-23. The stored Procedures for the Insert and Delete Actions
create procedure [chapter10].[InsertAuthorBook]
(@AuthorId int,@BookId int)
as
begin
insert into chapter10.AuthorBook(AuthorId,BookId) values (@AuthorId,@BookId)
end
go
create procedure [chapter10].[DeleteAuthorBook]
(@AuthorId int,@BookId int)
as
begin
delete chapter10.AuthorBook where AuthorId = @AuthorId and BookId = @BookId
end
2. 右擊模型設計視圖,選擇「從數據庫中更新模型」,選擇 Listing 10-23所建立的存儲過程,單擊「完成」,這樣就把存儲過程添加到了模型裏。
3. 目前版本的EF沒有爲一個關係插入和刪除操做映射的設計視圖,只能用手工去映射, 右擊.edmx 文件,選擇「打開方式」,選擇「XML (文本)編輯器」. 在<AssociationSetMapping> 標籤下插入 Listing 10-24所示的代碼(譯註:根據本身的例子命名修改代碼裏的)
Listing 10-24. Mapping the Stored Procedures to the Insert and Delete Actions for the Many-to-Many Association
<ModificationFunctionMapping>
<InsertFunction FunctionName="EFRecipesModel1009.Store.InsertAuthorBook">
<EndProperty Name="Author">
<ScalarProperty Name="AuthorId" ParameterName="AuthorId" />
</EndProperty>
<EndProperty Name="Book">
<ScalarProperty Name="BookId" ParameterName="BookId" />
</EndProperty>
</InsertFunction>
<DeleteFunction FunctionName="EFRecipesModel1009.Store.DeleteAuthorBook">
<EndProperty Name="Author">
<ScalarProperty Name="AuthorId" ParameterName="AuthorId" />
</EndProperty>
<EndProperty Name="Book">
<ScalarProperty Name="BookId" ParameterName="BookId" />
</EndProperty>
</DeleteFunction>
</ModificationFunctionMapping>
接下來Listing 10-25 代碼演示了插入和刪除操做.你能夠用Sql Profiler來查看當
InsertAuthorBook 和DeleteAuthorBook 存儲過程被EF在更新多對多關係時調用後生成的SQL語句
Listing 10-25. Inserting into the Model
class Program
{
static void Main(string[] args)
{
using (var context = new EFRecipesEntities1009())
{
context.Database.ExecuteSqlCommand("delete from chapter10.AuthorBook");
context.Database.ExecuteSqlCommand("delete from chapter10.book");
context.Database.ExecuteSqlCommand("delete from chapter10.Author");
var auth1 = new Author { Name = "Jane Austin" };
var book1 = new Book
{
Title = "Pride and Prejudice",
ISBN = "1848373104"
};
var book2 = new Book
{
Title = "Sense and Sensibility",
ISBN = "1440469563"
};
auth1.Books.Add(book1);
auth1.Books.Add(book2);
var auth2 = new Author { Name = "Audrey Niffenegger" };
var book3 = new Book
{
Title = "The Time Traveler's Wife",
ISBN = "015602943X"
};
auth2.Books.Add(book3);
context.Authors.Add(auth1);
context.Authors.Add(auth2);
context.SaveChanges();
context.Books.Remove(book1);
context.SaveChanges();
}
Console.WriteLine("\npress any key to exit...");
Console.ReadKey();
}
}
SQL Profiler裏跟蹤到的SQL語句以下(Listing 10-25)所示:
exec sp_executesql N'insert [Chapter10].[Author]([Name])values (@0)
select [AuthorId] from [Chapter10].[Author]
where @@ROWCOUNT > 0 and [AuthorId] = scope_identity()',N'@0 varchar(50)',@0='Jane Austin'
exec sp_executesql N'insert [Chapter10].[Author]([Name])values (@0)
select [AuthorId] from [Chapter10].[Author]
where @@ROWCOUNT > 0 and [AuthorId] = scope_identity()',N'@0 varchar(50)',
@0='Audrey Niffenegger'
exec sp_executesql N'insert [Chapter10].[Book]([Title], [ISBN])values (@0, @1)
select [BookId] from [Chapter10].[Book]
where @@ROWCOUNT > 0 and [BookId] = scope_identity()',N'@0 varchar(50),
@1 varchar(50)',@0='Pride and Prejudice',@1='1848373104'
exec sp_executesql N'insert [Chapter10].[Book]([Title], [ISBN])values (@0, @1)
select [BookId] from [Chapter10].[Book]
where @@ROWCOUNT > 0 and [BookId] = scope_identity()',N'@0 varchar(50),
@1 varchar(50)',@0='Sense and Sensibility',@1='1440469563'
exec sp_executesql N'insert [Chapter10].[Book]([Title], [ISBN])values (@0, @1)
select [BookId] from [Chapter10].[Book]
where @@ROWCOUNT > 0 and [BookId] = scope_identity()',N'@0 varchar(50),
@1 varchar(50)',@0='The Time Traveler''s Wife',@1='015602943X'
exec [Chapter10].[InsertAuthorBook] @AuthorId=1,@BookId=1
exec [Chapter10].[InsertAuthorBook] @AuthorId=1,@BookId=2
exec [Chapter10].[InsertAuthorBook] @AuthorId=2,@BookId=3
exec [Chapter10].[DeleteAuthorBook] @AuthorId=1,@BookId=1
exec sp_executesql N'delete [Chapter10].[Book] where ([BookId] = @0)',N'@0 int',@0=1
它是如何工做的?
爲把存儲過程映射到多對多關係中的插入和刪除操做, 咱們在數據庫中建立存儲過程,而後用存儲過程模型.因爲EF設計視圖不支持關係型的模型映射插入和刪除操做,咱們須要用XML編輯器直接打開.edmx 文件, 在Mappings 節點裏 <ModificationFunctionMapping> 標籤下,咱們添加了插入和刪除操做映射到存儲過程的代碼。
從Listing 10-25跟蹤生成的SQL,咱們能夠看到,不僅是插入或刪除 Author 和 Book 兩個表, 同時咱們也能夠看到,存儲過程在關係模型中插入和刪除操做時被使用。