LINQ to SQL Listing 2: Override Built-In Methods (VB.NET)

Typically, you need to implement several kinds of overrides for built-in LoadEntitySet() and LoadEntit­yRef() functions that you add to a partial DataContext file when you want to substitute stored procedures for LINQ to SQL's default dynamic prepared statements that populate associations. Neither VS 2008 beta 2 documentation nor the online help covers this syntax. Unfortunately, stored procedures are limited to a single entity per execution. Therefore, lazy loading or eager loading require the same number of server round-trips with stored procedures for EntitySets. Preloading EntityRefs can reduce round-trips to populate m:1 associations.

Partial Public Class NorthwindDataContext
	'Overrides to invoke stored procs instead of dynamic SQL

	'Following are for EntitySets
	Private Function LoadOrders(ByVal Cust As Customer) _
		As IEnumerable(Of Order)
		Return usp_GetOrdersByCustomerID(Cust.CustomerID)
	End Function

	Private Function LoadOrder_Details(ByVal Ord As Order) _
		As IEnumerable(Of Order_Detail)
		Return usp_GetOrder_DetailsByOrderID(Ord.OrderID)
	End Function

	'Following are for EntityRefs
	Private Function LoadCustomer(ByVal Ord As Order) _
		As Customer
		Return usp_GetCustomerByID(Ord.CustomerID). _
			FirstOrDefault()
	End Function

	Private Function LoadEmployee(ByVal Ord As Order) _
		As Employee
		Return usp_GetEmployeeByID(Ord.EmployeeID). _
			FirstOrDefault()
	End Function

	Private Function LoadShipper(ByVal Ord As Order) As Shipper
		Return usp_GetShipperByID(Ord.ShipVia). _
			FirstOrDefault()
	End Function
End Class
comments powered by Disqus

Featured

  • VS Code Keeps Eye on Costs in v1.126 Update

    Visual Studio Code 1.126 adds session-level Copilot cost information, continuing Microsoft's recent focus on helping developers monitor and manage usage-based GitHub Copilot billing.

  • Open VSX 1.0.0 Puts Focus on Open Extension Registry for VS Code Ecosystem

    Eclipse Open VSX has reached 1.0.0, highlighting its role as a vendor-neutral registry for VS Code-compatible extensions.

  • Infragistics Puts MCP Toolchain at Center of Ultimate 26.1

    Infragistics Ultimate 26.1 introduces the Ignite UI Enterprise MCP toolchain for AI-assisted app development across Angular, React, Web Components and Blazor.

  • VS Code 1.125 Adds Copilot Spend Meter After Billing Shock

    VS Code 1.125 adds in-editor visibility into additional Copilot budget usage as GitHub's AI-credit billing model continues to draw developer scrutiny.

Subscribe on YouTube