description | title | ms.date | dev_langs | ms.assetid | ||
---|---|---|---|---|---|---|
Learn more about: Adding Business Logic By Using Partial Methods |
Adding Business Logic By Using Partial Methods |
03/30/2017 |
|
3a73991e-fd4e-4610-93fb-7ced4dc6b7f9 |
You can customize Visual Basic and C# generated code in your [!INCLUDEvbtecdlinq] projects by using partial methods. The code that [!INCLUDEvbtecdlinq] generates defines signatures as one part of a partial method. If you want to implement the method, you can add your own partial method. If you do not add your own implementation, the compiler discards the partial methods signature and calls the default methods in [!INCLUDEvbtecdlinq].
Note
If you are using Visual Studio, you can use the Object Relational Designer to add validation and other customizations to entity classes.
For example, the default mapping for the Customer
class in the Northwind sample database includes the following partial method:
[!code-csharpDLinqOverrideDefault#2] [!code-vbDLinqOverrideDefault#2]
You can implement your own method by adding code such as the following to your own partial Customer
class:
[!code-csharpDLinqOverrideDefault#3] [!code-vbDLinqOverrideDefault#3]
This approach is typically used in [!INCLUDEvbtecdlinq] to override default methods for Insert
, Update
, Delete
, and to validate properties during object life-cycle events.
For more information, see Partial Methods (Visual Basic) or partial (Method) (C# Reference) (C#).
The following example shows ExampleClass
first as it might be defined by a code-generating tool such as SQLMetal, and then how you might implement only one of the two methods.
[!code-csharpDLinqSubmittingChanges#4] [!code-vbDLinqSubmittingChanges#4]
The following example uses the relationship between Shipper
and Order
entities. Note among the methods the partial methods, InsertShipper
and DeleteShipper
. These methods override the default partial methods supplied by [!INCLUDEvbtecdlinq] mapping.
[!code-csharpDLinqOverrideDefault#1] [!code-vbDLinqOverrideDefault#1]