Muokkaa

Jaa


Add a Finder method

To enable the Business Data Connectivity (BDC) service to display a list of entities in a web part or list, you must create a Finder method. A Finder method is a special method that returns a collection of entity instances. For more information, see Designing a Business Data Connectivity Model.

To create a Finder method

  1. On the BDC Designer, choose an entity.

    For more information, see How to: Add an entity to a model.

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

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

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

    Visual Studio adds a method, a return parameter, and a type descriptor.

  4. Configure the type descriptor as an entity collection type descriptor. For more information about how to create an entity collection type descriptor, see How to: Define the type descriptor of a parameter.

    Note

    You do not have to perform this step if you have added a Specific Finder method to the entity. Visual Studio uses the type descriptor that you defined in the Specific Finder method.

  5. In Solution Explorer, open the shortcut menu of the service code file that was generated for the entity, and then choose View Code. For more information about the service code file, see Create a business data connectivity model.

  6. Add code to the Finder method. This code performs the following tasks:

    • Retrieves data from a data source.

    • Returns a list of entities to the BDC service.

      The following example returns a collection of Contact entities by using data from the AdventureWorks sample database for SQL Server.

    Note

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

    public static IEnumerable<Contact> ReadList()
    {
        const string ServerName = "MySQLServerName";
        AdventureWorksDataContext dataContext = new AdventureWorksDataContext
              ("Data Source=" + ServerName + ";" +
               "Initial Catalog=AdventureWorks;Integrated Security=True");
    
        IEnumerable<Contact> Contacts =
            from contacts in dataContext.Contacts.Take(20)
            select contacts;
        return Contacts;
    
    }