In addition to building your custom object in your own cs file and then tying that class·to the DataContext that you have built, you can also use the O/R Designer in Visual Studio 2008 to build your class files, When you use Visual Studio in this manner, it will create the appropriate cs file on your behalf, but by using the O/R Designer, you will also have a visual representation of the class file and any possible relationships that you have established.
When viewing the Designer view of your .dbml file, you will notice that there are three items present in the toolbox These items are Class, Association, and Inheritance, For an example of this, take the Class object from the toolbox and drop it onto the design surface, You will be printed with an image of the generic class as shown in Figure 27-7.
From here, you can now click the Classl name and rename this class to Customer Right-clicking next to the name enables you to add properties to the class file by selecting Add → Property from the provided menu For this example, give the Customer class three properties – CustomerID, CompanyName,and Countzy, If you highlight the CustomerID property, you will be able to configure the property from the Properties dialog in Visual Studio and change the Primary Key setting from False to True, You also want to highlight the entire class and go to the Properties dialog and change the Source property to Customers because this is the name of the table from which this Customer object needs to work. After this is all done, you will have a visual representation of the class as shown in Figure 27-8
As y~u can see from this image, the CustomerID property is properly represented with a primary key icon next to the name. With this in place, you can expand the plus sign next to the Northwind, dbml file and you will find two files here -Northwind.dbml.layout and Northwind.designer .cs. The Northwind. dbml.layout file is an XML file that helps Visual Studio with the visual representation shown in the O/R Designer, The file that is the most important is the Northwind. designer. cs file, This.is the Customer class file that was created on your behalf. When you open this file, you are able to see what Visual Studio created for you.
First, you will lmd the Customer class file within the code of the page:
public partial class Customer INotifyPropertyChanging,
II Code removed for clarity
The Customer class is the name of the class according to what you provided in the designer, The class comes with the Table attribute and provides a name value of Customers because this is the name of the database that this object will need to work with when connecting to the Northwind database.
Within the Customer class, you will find the three properties that you defined Presented here is just one of the properties – CustomerID:
[Column(Storage=”_CustomerIO”, CanBeNull=false, IsPrimaryKey=truel]
public string CustomerIO
. if «this._CustomerID != value)) (
this._CustomerID = value;
this. OnCustomerIDChanged(); •
Similar to when you built a class for yourself from the earlier example, the properties defined use the column attribute and some of the properties available to this attribute, You can see that the primary key setting is set using the IsPrimaryKey item.
In addition to the Customer” class, you will find that a class inheriting from the DataContext object is also within the created file:
public partial class NorthwindDataContext : System.Data.Linq.DataContext
II Code removed for clarity
This DataContext object, NorthwindDataContext, allows you to connect to the Northwind database and class the Customers table as was accomplished in the previous examples .
You will find that using the 0/R Designer is a process that can make the creation of your database object class files simple and straightforward, However, at the same time, if you want complete control, you can code up everything yourself and get the results you are after.