asp.net 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.
image

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

Now we have a data source, create a asp.net web site to consume the data.
Now add a asp.net web form application to your solution.
image
Press F5 and you should see a default web site. Close it.
Right click the project, add new item –>
image
Since you already have a DB, select generate from DB option.
image
Set up connection strings, and give the name “Entities”
image
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.
image

When you scope to the EDMX, you see this:
image
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!
image
Now you have this in web form:
image
Now edit the code behind.
image
Press F5 –>
image
You can also use EntityDataSource as a data source.
Drag to the form:
image
Select the Context:
image
image
Then set the data source of the repeater to this new data source.
image
You need to comment out the code in LOAD:
image
or else you will get a message that says it has already been defined.
Press F5, you get the same result using the EntityDataSource:
image

End of Chapter 1 – Page 25

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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