共用方式為


How to: Add a Creator Method

A Creator method adds new data to the data source of an entity. The Business Data Connectivity (BDC) service calls this method when users choose the New Item button on the Ribbon of a list that is based on the model. For more information, see Designing a Business Data Connectivity Model.

To add a Creator 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 that window, see BDC Model Design Tools Overview.

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

    Visual Studio adds the following elements to the model, and these elements appear in the BDC Method Details window.

    • A method named Create.

    • An input parameter for the method.

    • A return parameter for the method.

    • Type descriptors for the parameters.

    • A method instance for the method.

    For more information, see Designing a Business Data Connectivity Model.

  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 Creating a Business Data Connectivity Model.

  5. Add code to the Creator method that adds data to the data source. The following example adds a contact to the AdventureWorks sample database for SQL Server.

    Note

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

    Public Shared Function Create(ByVal newContact As Contact) As Contact
        Const ServerName As String = "MySQLServerName" 
        Dim dataContext As AdventureWorksDataContext = _
            New AdventureWorksDataContext("Data Source=" & ServerName & _
                ";Initial Catalog=AdventureWorks;Integrated Security=True")
    
        Dim TempContact As New Contact()
    
        With TempContact
            .FirstName = newContact.FirstName
            .LastName = newContact.LastName
            .EmailAddress = newContact.EmailAddress
            .Phone = newContact.Phone
            .EmailPromotion = newContact.EmailPromotion
            .NameStyle = newContact.NameStyle
            .PasswordHash = newContact.PasswordHash
            .PasswordSalt = newContact.PasswordSalt
            .ModifiedDate = DateTime.Now
            .rowguid = Guid.NewGuid()
        End With
    
        dataContext.Contacts.InsertOnSubmit(TempContact)
        dataContext.SubmitChanges()
        Return TempContact
    
    End Function
    
    public static Contact Create(Contact newContact)
    {
        const string ServerName = "MySQLServerName";
        AdventureWorksDataContext dataContext = new AdventureWorksDataContext
              ("Data Source=" + ServerName + ";" +
               "Initial Catalog=AdventureWorks;Integrated Security=True");
    
        Contact contact = new Contact();
    
        contact.FirstName = newContact.FirstName;
        contact.LastName = newContact.LastName;
        contact.EmailAddress = newContact.EmailAddress;
        contact.Phone = newContact.Phone;
        contact.EmailPromotion = newContact.EmailPromotion;
        contact.NameStyle = newContact.NameStyle;
        contact.PasswordHash = newContact.PasswordHash;
        contact.PasswordSalt = newContact.PasswordSalt;
        contact.ModifiedDate = DateTime.Now;
        contact.rowguid = Guid.NewGuid();
    
        dataContext.Contacts.InsertOnSubmit(contact);
        dataContext.SubmitChanges();
        return contact;
    
    }
    

See Also

Tasks

How to: Add a Finder Method

How to: Add a Specific Finder Method

How to: Add a Deleter Method

How to: Add an Updater Method

How to: Add a Parameter to a Method

How to: Define a Method Instance

Concepts

BDC Model Design Tools Overview

Other Resources

Designing a Business Data Connectivity Model