Database Reference
In-Depth Information
Listing 10-24. Mapping the Stored Procedures to the Insert and Delete Actions for the Many-to-Many Association
<ModificationFunctionMapping>
<InsertFunction FunctionName="EF6RecipesModel.Store.InsertAuthorBook">
<EndProperty Name="Author">
<ScalarProperty Name="AuthorId" ParameterName="AuthorId" />
</EndProperty>
<EndProperty Name="Book">
<ScalarProperty Name="BookId" ParameterName="BookId" />
</EndProperty>
</InsertFunction>
<DeleteFunction FunctionName="EF6RecipesModel.Store.DeleteAuthorBook">
<EndProperty Name="Author">
<ScalarProperty Name="AuthorId" ParameterName="AuthorId" />
</EndProperty>
<EndProperty Name="Book">
<ScalarProperty Name="BookId" ParameterName="BookId" />
</EndProperty>
</DeleteFunction>
</ModificationFunctionMapping>
The code in Listing 10-25 demonstrates inserting into and deleting from the model. As you can see from the SQL
Profiler output that follows, our InsertAuthorBook and DeleteAuthorBook stored procedures are called when Entity
Framework updates the many-to-many association.
Listing 10-25. Inserting into the Model
using (var context = new EF6RecipesContext())
{
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.Delete(book1);
context.SaveChanges();
}
 
Search WWH ::




Custom Search