2013年6月4日 星期二

DbContext APIs (for Insert, Update, Delete and Get)

The model of SalesOrder.edmx

image

image

SalesOrder.Context.cs

SalesOrderDBContext : DbContext

image

 

GetCustomers()

Context.Entity.ToList()

image

 

GetCustomer(int id)

Context.Entity.Find(primary_key)

image

TOP (2): to make sure Find() is always return 1 record, so it will raise an exception if SQL server return 2 records

image

 

InsertCustomer()

1. Context.Entity.Add(item)

2. Context.SaveChanges()

3. Able to get primary key after insertion

image

INSERT and SELECT are in a single transaction

image

1. For context.CustomerSet.Add(customer);

2. For customer.Id

image

 

UpdateCustomer()

image

SELECT and UPDATE are in different transactions

image

Hardcode name for WHERE condition

image

image

 

DeleteCustomers()

Context.Entity.Remove(item)

image

SELECT and DELETE are in different transactions

image

image

image

沒有留言:

張貼留言