通过使用存储过程自定义操作

存储过程代表用于重写默认行为的常见方法。 本主题中的示例演示了如何将生成的方法包装用于存储过程,以及如何直接调用存储过程。

如果使用的是 Visual Studio,可以使用对象关系设计器指定存储过程来执行插入、更新和删除。

备注

若要读回数据库生成的值,请在存储过程中使用输出参数。 如果无法使用输出参数,则编写分部方法实现,而不是依靠 O/R 设计器生成的重写。 在成功完成 INSERTUPDATE 操作后,必须将映射到数据库生成的值的成员设置为适当的值。 有关详细信息,请参阅开发人员在重写默认行为中的责任

示例 1

在下面的示例中,假定 Northwind 类包含两个方法,这两个方法可用来调用要用于派生类中的重写的存储过程。

[Function()]
public IEnumerable<Order> CustomerOrders(
    [Parameter(Name = "CustomerID", DbType = "NChar(5)")]
    string customerID)
{
    IExecuteResult result = this.ExecuteMethodCall(this,
        ((MethodInfo)(MethodInfo.GetCurrentMethod())),
        customerID);
    return ((IEnumerable<Order>)(result.ReturnValue));
}

[Function()]
public IEnumerable<Customer> CustomerById(
    [Parameter(Name = "CustomerID", DbType = "NChar(5)")]
    string customerID)
{
    IExecuteResult result = this.ExecuteMethodCall(this,
        ((MethodInfo)(MethodInfo.GetCurrentMethod())),
        customerID);
    return (IEnumerable<Customer>)(result.ReturnValue);
}
<[Function]()> _
Public Function CustomerOrders( _
    <Parameter(Name:="CustomerID", DbType:="NChar(5)")> ByVal _
    customerID As String) As IEnumerable(Of Order)

    Dim result As IExecuteResult = Me.ExecuteMethodCall(Me, _
        (CType(MethodInfo.GetCurrentMethod(), MethodInfo)), _
        customerID)
    Return CType(result.ReturnValue, IEnumerable(Of Order))
End Function

<[Function]()> _
Public Function CustomerById( _
    <Parameter(Name:="CustomerID", DbType:="NChar(5)")> ByVal _
        customerID As String) As IEnumerable(Of Customer)

    Dim result As IExecuteResult = Me.ExecuteMethodCall(Me, _
        CType(MethodInfo.GetCurrentMethod(), MethodInfo), _
        customerID)

    Return CType(result.ReturnValue, IEnumerable(Of Customer))
End Function

示例 2

下面的类将这些方法用于重写。

public class NorthwindThroughSprocs : Northwind
{

    public NorthwindThroughSprocs(string connection) :
        base(connection)
    {
    }

    // Override loading of Customer.Orders by using method wrapper.
    private IEnumerable<Order> LoadOrders(Customer customer)
    {
        return this.CustomerOrders(customer.CustomerID);
    }
    // Override loading of Order.Customer by using method wrapper.
    private Customer LoadCustomer(Order order)
    {
        return this.CustomerById(order.CustomerID).Single();
    }
    // Override INSERT operation on Customer by calling the
    // stored procedure directly.
    private void InsertCustomer(Customer customer)
    {
        // Call the INSERT stored procedure directly.
        this.ExecuteCommand("exec sp_insert_customer …");
    }
    // The UPDATE override works similarly, that is, by
    // calling the stored procedure directly.
    private void UpdateCustomer(Customer original, Customer current)
    {
        // Call the UPDATE stored procedure by using current
        // and original values.
        this.ExecuteCommand("exec sp_update_customer …");
    }
    // The DELETE override works similarly.
    private void DeleteCustomer(Customer customer)
    {
        // Call the DELETE stored procedure directly.
        this.ExecuteCommand("exec sp_delete_customer …");
    }
}
Public Class NorthwindThroughSprocs : Inherits Northwind
    Sub New()
        MyBase.New("")
    End Sub
    ' Override loading of Customer.Orders by using method wrapper.
    Private Function LoadOrders(ByVal customer As Customer) As  _
        IEnumerable(Of Order)
        Return Me.CustomerOrders(customer.CustomerID)
    End Function

    ' Override loading of Order.Customer by using method wrapper.
    Private Function LoadCustomer(ByVal order As Order) As Customer
        Return Me.CustomerById(order.CustomerID).Single()
    End Function

    ' Override INSERT operation on Customer by calling the
    ' stored procedure directly.
    Private Sub InsertCustomer(ByVal customer As Customer)
        ' Call the INSERT stored procedure directly.
        Me.ExecuteCommand("exec sp_insert_customer …")
    End Sub

    ' The UPDATE override works similarly, that is, by
    ' calling the stored procedure directly.
    Private Sub UpdateCustomer(ByVal original As Customer, ByVal _
        current As Customer)
        ' Call the UPDATE stored procedure by using current
        ' and original values.
        Me.ExecuteCommand("exec sp_update_customer …")
    End Sub

    ' The DELETE override works similarly.
    Private Sub DeleteCustomer(ByVal customer As Customer)
        ' Call the DELETE stored procedure directly.
        Me.ExecuteCommand("exec sp_delete_customer …")
    End Sub
End Class

示例 3

您可以完全像使用 NorthwindThroughSprocs 一样使用 Northwnd

NorthwindThroughSprocs db = new NorthwindThroughSprocs("");
var custQuery =
    from cust in db.Customers
    where cust.City == "London"
    select cust;

foreach (Customer custObj in custQuery)
    // deferred loading of cust.Orders uses the override LoadOrders.
    foreach (Order ord in custObj.Orders)
        // ...
        // Make some changes to customers/orders.
        // Overrides for Customer are called during the execution of the
        // following:
        db.SubmitChanges();
Dim db As New NorthwindThroughSprocs()
Dim custQuery = From cust In db.Customers _
                Where cust.City = "London" _
                Select cust

For Each custObj In custQuery
    ' Deferred loading of cust.Orders uses the override LoadOrders.
    For Each ord In custObj.Orders
        ' ...
        ' Make some changes to customers/orders.
        ' Overrides for Customer are called during the execution 
        ' of the following:
        db.SubmitChanges()
    Next
Next

请参阅