2013年6月4日 星期二

Building and Inserting Graphs (with Tracked Objects)

A graph tree of our example, Customer is the root

image

 

Let’s see how to create a customer with order detail data

1. A function, GetProducts() to get existing product setup in the database

image

2. A function, CreateCustomerWithOrder() to create a Customer and its ContactDetail record

3. Before that, call GetProducts() function to get existing product setup

image

4. Within the same function, CreateCustomerWithOrder(), new an Order with 2 new LineItem (but using existing product)

5. Add the Order into Customer

6. Add the Customer into context

image

Following picture shows objects tracked by DbContext

image

Put breakpoint on using (var context = new SalesOrderDBContext()) to see customer class

1. Customer is there

2. ContactDetail is there

3. Order is there

4. LineItem is there

image

5. Product is also there, but it’s not what we want

image

image

 

The solution to solove it by using Product.ProdictID

image

This time, no product insert only LineItem

image

 

By the way, sometimes we will encounter error when Context.SaveChanges(), that is because one or more properties we did not assign them the value.

image

The error message is DbEntityValidationException was unhandled

image

 

Additional information:

image

Disconnected graphs:

image

沒有留言:

張貼留言