Relationships

The framework supports the many relationship type of a modern database. The generated code is just Entity Framework so it should be familiar to anyone who has used it. The code generated is based on the relation type as well as meta data you place on it.

1->N
A one-to-many relationship generates code that is immediate readable as such. For example, in a Customer->Order table structure, you would get code as follows. Notice that the list of orders is recognizable as plural because it is a list.

using (var context = new NorthwindEntities())
{
	var customer = context.Customer.FirstOrDefault();
	foreach (var order in customer.OrderList)
	{
		//..do something..
	}
				
}

N->1
The reverse of the one-to-many relation is the many-to-one. This is essentialy the back pointer to the parent object. You will notice that the order has one customer associated with it. If the Order allows null for the customer id key in the relationship, then the CustomerItem can be null, otherwise not.

using (var context = new NorthwindEntities())
{
	var order = context.Order.FirstOrDefault();
	var customer = order.Customer;
}

N->M
A relationship that relational databases do not handle is the many-to-many. The standard way of handling this functionality is to add an intermediary table with a relationship to each of the primary tables. This works but clouds code as it is a convenience not part f your model. The framework handles this by using the tertiary table paradigm but hiding its implementation details from you. So each of your primary objects have an object list on them not a pointer to a "non-real" table.

Lets say we have an Order and Product table. An Order has many Products and a Products can be assigned to many Orders. In our model, we create both entities and also the intermediary OrderProduct table. We map the primary keys of each primary table to the middle table to get the standard way of associating data. However now we mark the middle table with the IsAssociative meta data. Now when the code is generated we can use code as follows.

using (var context = new NorthwindEntities())
{
	var order = context.Order.FirstOrDefault();
	var product = order.ProductList.FirstOrDefault();
	var orderList = product.OrderList.FirstOrDefault();
}

Notice that you never see the intermediary table. As far as a software developers is concerned it does not exist.

1->1
You may wish to create a one-to-one table relationship. This can be done by giving both tables the same type of primary key (type and cardinality) and creating an association. This would normally be a 1->N relation, but if the referenced columns are the primary keys of both table and the child table's columns are marked as with the unique metadata the code is generated with a single Item property not a List property.

Assume we have a Customer->CustomerResume relation and we make this a one-to-one relation. The code can them be used as follows.

using (var context = new NorthwindEntities())
{
	var customer = context.Customer.FirstOrDefault();
	var resume = customer.Resume;
}

Last edited May 8, 2012 at 1:24 AM by codetools, version 5

Comments

No comments yet.