4.5 database (1)

For a new DB, the code-centric is simpler than it is for an existing database. Three ways of data access development: 
(1) Database first – reverse engineer the Entity Model from the database schema. generate the entity class automatically from the EM using tools.
(2) Model first – Generate the Entity Model in the designer. Generate the database schema and entity classes from the EM.
(3) Code first – Define model and relationships in code. Create a new DB at runtime.

ADO.NET classes
(1) connect to DB using .NET 4.5 data providers
(2) execute commands
(3) retrieve results

The list of ASO.NET data sources: MS SQL, Oracle.

WCF Data services enables CRUD operations on data using the Open Data Protocol (ODATA). WCF Data services will allow REST-style access to data models. Supports JSON and ATOM.
Entity Framework 5 – supports table-value functions and importing Stored Procs.

Create a sample project in VS 2012. Open VS 2012, in server explorer, add a new SLQ DB.

Create a new DB: Gadgets
Now add some data. Right click the table, and View Data.
If you want to make the Id field auto-generated:

Now we have a data source, create a web site to consume the data.
Now add a web form application to your solution.
Press F5 and you should see a default web site. Close it.
Right click the project, add new item –>
Since you already have a DB, select generate from DB option.
Set up connection strings, and give the name “Entities”
In next screen, select the table, and give a Model name space of GadgetsModel.
In your solution explorer, if you only want to display elements related to a specific item (such as a folder), then right click the element, and click “Scope to this.”

Use the back arrow to get out of the “scope” mode.

When you scope to the EDMX, you see this:
Add a web form to your web project called GadgetStoreView.aspx
Drag a repeater to the web form. Specify in the property window the ItemType: image

After that you can manually edit the repeater with intellisense support!
Now you have this in web form:
Now edit the code behind.
Press F5 –>
You can also use EntityDataSource as a data source.
Drag to the form:
Select the Context:
Then set the data source of the repeater to this new data source.
You need to comment out the code in LOAD:
or else you will get a message that says it has already been defined.
Press F5, you get the same result using the EntityDataSource:

End of Chapter 1 – Page 25


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s