A graph tree of our example, Customer is the root
Let’s see how to create a customer with order detail data
1. A function, GetProducts() to get existing product setup in the database
2. A function, CreateCustomerWithOrder() to create a Customer and its ContactDetail record
3. Before that, call GetProducts() function to get existing product setup
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
Following picture shows objects tracked by DbContext
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
5. Product is also there, but it’s not what we want
The solution to solove it by using Product.ProdictID
This time, no product insert only LineItem
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.
The error message is DbEntityValidationException was unhandled
Additional information:
Disconnected graphs:
沒有留言:
張貼留言