Customizing The Insert, Update, and Delete Behavior of Entity Classes
Customizing The Insert, Update, and Delete Behavior of Entity Classes
This walkthrough provides the steps that you must follow to override the default
LINQ to SQL runtime behavior for saving data back to a database by using stored
procedures.
During this walkthrough, you will learn how to perform the following tasks:
Create a new Windows Forms application and add a LINQ to SQL file to it.
Create an object data source that references the LINQ to SQL Customer class.
Prerequisites
To complete this walkthrough, you need the following:
Access to the SQL Server version of the Northwind sample database. For
more information, see How to: Install Sample Databases.
The O/R Designer is supported in Visual Basic and C# projects. Therefore, create
the new project in one of these languages.
3. Click the Windows Forms Application template and click OK. For more
information, see Developing Client Applications.
The UpdatingwithSProcsWalkthrough project is created and added to Solution
Explorer.
4. On the Project menu, click Add New Item.
5. Click the LINQ to SQL Classes template and type Northwind.dbml in the Name
box.
6. Click Add.
An empty LINQ to SQL Classes file (Northwind.dbml) is added to the project,
and the O/R Designer opens.
8. Click Finish to create the data source and add the Customer entity class to
the Data Sources window.
4. Add the following code to the form, global to the form, outside any specific
method, but inside the Form1 class:
C#
private NorthwindDataContext northwindDataContext1
= new NorthwindDataContext();
5. Create an event handler for the Form_Load event and add the following code
to the handler:
C#
customerBindingSource.DataSource
= northwindDataContext1.Customers;
1. Open the LINQ to SQL file in the O/R Designer. (Double-click the
Northwind.dbml file in Solution Explorer.)
2. In Server Explorer/Database Explorer, expand the Northwind databases
Stored Procedures node and locate the InsertCustomers, UpdateCustomers,
and DeleteCustomers stored procedures.
3. Drag all three stored procedures onto the O/R Designer.
The stored procedures are added to the methods pane as DataContext
methods. For more information, see DataContext Methods (O/R Designer).
4. Select the Customer entity class in the O/R Designer.
5. In the Properties window, select the Insert property.
6. Click the ellipsis (...) next to Use Runtime to open the Configure Behavior
dialog box.
7. Select Customize.
8. Select the InsertCustomers method in the Customize list.
9. Click Apply to save the configuration for the selected Class and Behavior.
Note
You can continue to configure the behavior for each class/behavior combination as
long as you click Apply after you make each change. If you change the class or
behavior before you click Apply, a warning dialog box providing an opportunity to
apply any changes will appear.
By default, method arguments will map to class properties when the names match.
If property names are changed and no longer match between the table and the
entity class, you might have to select the equivalent class property to map to if the
O/R Designer cannot determine the correct mapping. Additionally, if method
arguments do not have valid class properties to map to, you can set the Class
Properties value to (None).
14.Click Apply to save the configuration for the selected Class and Behavior.
15.Select Delete in the Behavior list.
16.Select Customize.
17.Select the DeleteCustomers method in the Customize list.
18.Map the Original_CustomerID method argument to the CustomerID (Original)
class property.
19.Click OK.
Note
Although it is not an issue for this particular walkthrough, it is worth noting that
LINQ to SQL handles database-generated values automatically for identity (autoincrement), rowguidcol (database-generated GUID), and timestamp columns during
Inserts and Updates. Database-generated values in other column types will
unexpectedly result in a null value. To return the database-generated values, you
should manually set IsDbGenerated to true and AutoSync to one of the following:
Always, OnInsert, or OnUpdate.