Database Reference
In-Depth Information
You should always specify transaction isolation level when you work with a TransactionScope class.
it uses a SERIALIZABLE isolation level by default unless you override it.
Important
You can read more about transaction management in entity Framework at:
http://msdn.microsoft.com/en-us/data/dn456843 .
Note
Executing Stored Procedures and Queries
You can run queries and stored procedures in Entity Framework by using the ExecuteSqlCommand method of the
Database class, accessing it through the property exposed by the DbContext class. This extremely important feature
allows you to avoid auto-generated SQL in performance-critical use cases.
Listing 16-7 shows you how to call a stored procedure within an Entity Framework context.
Listing 16-7. Executing a stored procedure in Entity Framework
using (var context = new EFDbContext())
{
context.Database.ExecuteSqlCommand("exec dbo.ReconcileOrder @CustomerId",
new SqlParameter("@CustomerId", SqlDbType.Int) {Value = 50});
}
The ability to execute queries is also beneficial during deletion of multiple rows when foreign key constraints are
involved. We will review such an example later in the chapter.
Data Loading
Entity Framework supports partial loading when a subset of the entity attributes are selected. Listing 16-8 demonstrates
such an example. The CustNames is the list of anonymous type objects with two attributes: FirstName and LastName .
Listing 16-8. Partial load: Client code
var custNames = context.Customers
.Where(t => t.FirstName == "John")
.Select(t => new{t.FirstName,t.LastName});
The SQL generated by Entity Framework selects only two columns, as shown in Listing 16-9.
Listing 16-9. Partial load: Generated SQL
SELECT
1 AS [C1],
[Extent1].[FirstName] AS [FirstName],
[Extent1].[LastName] AS [LastName]
FROM [dbo].[Customers] AS [Extent1]
WHERE N'John' = [Extent1].[FirstName]
 
 
Search WWH ::




Custom Search