顯示具有 Entity Framework 標籤的文章。 顯示所有文章
顯示具有 Entity Framework 標籤的文章。 顯示所有文章

2013年7月1日 星期一

Entity Framework–DB First (DbContext Name)

By default, the name is XXXEntities, suggest to change to XXXContext, because it represents followings:

1. The Connection String in Web.config

2. The Entity Container Name

3. The Class Name of DbContext

image

 

The Model Namespace, not that important

image

 

Above 2 names will appear in the property window

image

 

This shows the name of DbContext class defined before

image

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

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

2013年6月3日 星期一

Entity Framework–Model First, Part 4 (Diagram & Color)

We are able to separate all entities into several diagrams, it is no such limitation to put all in one model design diagram.

First, open Model Browser by VIEW –> Other Windows –> Entity Data Model Browser

image

Add a new diagram named Product, and add 2 entities Product and Category by follow the steps in this blog

image

Switch to previous diagram, Diagram1, Add a new Association

image

In this window, we can also see Product & Category entities even they are not belong to this diagram

image

After that, we see Product navigation property has been added into Order

image

Order has been added into Product as well, but different diagram

image

 

In the meanwhile, we can change the color of entity by changing its Fill Color property to other color

image

The result will be look like this if we change to red color

image

 

Let’s see what happen if we Generate Database from Model… again with new add diagram and association in the model

image

Obviously, the script drop all existing Foreign Key constraints and all existing Tables

image

2013年6月2日 星期日

Entity Framework–Model First, Part 3 (Execute script to create database)

In previous step(Generate database schema), we successfully generated xxx.edmx.sql script file

image_thumb[35]

 

Look at table definition, we can see what we did changes

1. The Max Length of Industry = 50

2. The Type of OrderDate & ShipToAddress, datetime & geography

image_thumb[36]

The Primary Key constraints

image_thumb[37]

The Foreign Key constraints

image_thumb[38]

 

Now if everything is ok, we can then right-click to Execute the script

image_thumb[40]

Make sure again we are connect to right destination, click Connect button for next step

image_thumb[23]

Once done, we are able to see the successfully completed message

image_thumb[41]

The final step, we can open Server Explorer to check the database and tables are all been created

image_thumb[25]