BCS model – a simple read only model

18 Aug

In the first part, BCS model – some basics , I talked about the items Visual Studio creates for you, the meaning of these items and some about renaming files. The next step is to create a simple model to show the functionality of the model.  

Just create a new project in Visual Studio based on the template ‘Business Data Connectivity Model’ and give the project a name. In the next screen you can see a BSC model can be deployed only as a farm solution, not a sandboxed solution, and select the site for debugging.  

Rename the class Entity1 to Customer, rename the file Entity1.cs to Customer.cs and update the design view of the model (.bdcm) by setting the name of the entity to Customer. By the last rename the Entity1Service is automatically renamed to CustomerService. A quick refresh of the previous post: ‘The entity on the design has a custom property which points to the entity service. With this connection the code in the service can be adjusted to the changes made in the design surface or BDC Method Details.‘

Delete the methods at the BDC Method Details window and clean the CustomerService by deleting the methods.
Change the Customer entity to:

    public partial class Customer
    {
        public int CustomerId { get; set; }
        public string CustomerName { get; set; }
        public string CustomerCity { get; set; }
    }

And update the design view by renaming Identifier1 to CustomerId and change its type to System.Int32. All in sync now.  

Now let’s create the methods for the entity and start with the simplest one, a Finder method to list all the Customers.
In the BDC Method Details window select ‘Add a method’ and ‘Create Finder Method’. The ReadList method is created now with a return type descriptor with the name CustomerList. Since this is just a name, check the Type Name in the properties window: System.Collections.Generic.IEnumerable`1[System.String]
We have to change the generic list of strings to a generic list of Customers by selecting the arrow next to the type, select the tab Current Project and select the Customer entity.

The type name changed now to:
System.Collections.Generic.IEnumerable`1[[ITIdea.BCSBasics.BdcModel1.Customer, BdcModelSub]]
And the CustomerService class is updated as well.The return type of the method is a generic list of Customer objects. The next thing we have to do is to specify our Customer object to return the fields representing the Customer object. Therefor we have to add additional type descriptors at the Customer return type at the BDC Explorer. 

Select in the BDC Explorer to the Customer type descriptor of the ReadList method. Right click and select Add Type Descriptor. Change the name to CustomerId and the type to System.Int32, set the Identifier property to CustomerId and ReadOnly to True. Add two others with the name CustomerName and type System.String and the name CustomerCity and type System.String.

When you don’t define the object and you create a list based on the entity (External Content Type) you will receive the following message when displaying the list:

To retrieve one customer at a time we create a Specific Finder method.  

In the BDC Method Details window select ‘Add a method’ and ‘Create Specific Finder Method’. The ReadItem method is created with two parameters: a return parameter customer and an input parameter customerId. The type descriptor’s type name of the customer parameter is set the System.String at the properties window, change this to return a Customer type.
The other type descriptors are already there because we defined them at the ReadList method.  

The last thing we have to do now is to write some implementation of our methods ReadItem and ReadList. Now change these method to return just one Customer at ReadItem and a list of Customers at ReadList.  (I will  leave this for your own fantasy, I use a Linq to Sql class to quickly connect to a table in a database)

Deploy the project and create a list based on this External Content type.

As you notice you can view all the Customers you defined in the ReadList method and you can view the details of one customer.
Of course you can change the displayname of the columns to whatever you want by changing the Default Display Name at the properties of a specific Type Descriptor in Visual Studio.  

In case you don’t see the contents of the list, check BCS – possible errors.

This is all great but it would be really nice to add new items and edit existing ones –> will be my next post.

2 Replies to “BCS model – a simple read only model

Comments are closed.