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

  • Full Stack Hands-On Development with .NET

    In the fast-paced realm of modern software development, proficiency across a full stack of technologies is not just beneficial, it's essential. Microsoft has an entire stack of open source development components in its .NET platform (formerly known as .NET Core) that can be used to build an end-to-end set of applications.

  • .NET-Centric Uno Platform Debuts 'Single Project' for 9 Targets

    "We've reduced the complexity of project files and eliminated the need for explicit NuGet package references, separate project libraries, or 'shared' projects."

  • Creating Reactive Applications in .NET

    In modern applications, data is being retrieved in asynchronous, real-time streams, as traditional pull requests where the clients asks for data from the server are becoming a thing of the past.

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

Subscribe on YouTube