Partager via


How to: Add a Deleter method

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

You can enable an end user to delete a data record from an external list on a SharePoint site by adding a Deleter method to the model. For more information, see Design a business data connectivity model.

To create a Deleter method

  1. On the BDC Designer, choose an entity.

  2. On the menu bar, choose View > Other Windows > BDC Method Details.

    The BDC Method Details window opens. For more information about this window, see BDC model design tools overview.

  3. In the Add a Method list, choose Create a Deleter Method.

    Visual Studio adds the following elements to the model. These elements appear in the BDC Method Details window.

  4. In Solution Explorer, open the shortcut menu of the service code file that was generated for the entity, and then choose View Code.

    The entity service code file opens in the Code Editor. For more information about the entity service code file, see Create a business data connectivity model.

  5. Add code to the Deleter method to delete a record. The following example deletes a line item from a sales order by using the AdventureWorks sample database for SQL Server.

    Note

    The method in this example uses two input parameters.

    Note

    Replace the value of the ServerName field with the name of your server.

    public static void Delete(int salesOrderID, int salesOrderDetailID)
    {
        const string ServerName = "MySQLServerName";
        AdventureWorksDataContext dataContext = new AdventureWorksDataContext
              ("Data Source=" + ServerName + ";" +
               "Initial Catalog=AdventureWorks;Integrated Security=True");
    
        SalesOrderDetail SalesOrderDetail =
               (from SalesOrderDetails in dataContext.SalesOrderDetails.AsEnumerable().Take(20)
                where SalesOrderDetails.SalesOrderID == salesOrderID &&
                SalesOrderDetails.SalesOrderDetailID == salesOrderDetailID
                select SalesOrderDetails).Single();
    
        dataContext.SalesOrderDetails.DeleteOnSubmit(SalesOrderDetail);
        dataContext.SubmitChanges();
    }
    
    Public Shared Sub Delete(ByVal salesOrderID As Integer, ByVal salesOrderDetailID As Integer)
        Const ServerName As String = "MySQLServerName"
        Dim dataContext As AdventureWorksDataContext = _
            New AdventureWorksDataContext("Data Source=" & ServerName & _
                ";Initial Catalog=AdventureWorks;Integrated Security=True")
    
        Dim SalesOrderDetail As SalesOrderDetail = _
            (From SalesOrderDetails In dataContext.SalesOrderDetails.AsEnumerable().Take(20) _
            Where SalesOrderDetails.SalesOrderID = salesOrderID And _
                  SalesOrderDetails.SalesOrderDetailID = salesOrderDetailID _
            Select SalesOrderDetails).Single()
    
        dataContext.SalesOrderDetails.DeleteOnSubmit(SalesOrderDetail)
        dataContext.SubmitChanges()
    
    End Sub
    

See also