Search...

Monday, March 19, 2012

Entity data model tutorial


Open Visual Studio and create an ASP.NET Web Application project named BusinessObjects.
a. Click File, point to New, and click Project.



b. Select the Console Application template and change the project name to   EntityframeworkExample.


Right click on your solution navigate to Add further navigate to New item....


Choose Ado.Net Entity Data Model and name it SoftwareKaffee.edmx


The Entity Data Model Wizard shows up and you now can use this to query your database and generate the model diagram, as long as you supply it with the right credentials. In the Wizard, click "Generate from Database" and click Next.


Supply it with the right server name, authentication, credentials, and the database name


Select your tables


This is a graphical representation of the Entity Data Model (EDM) that's generated by the wizard.
 


***********************************************************************

using System;
using System.Linq;

namespace BusinessObjects
{
    class Program
    {
        static void Main(string[] args)
        {
            softwarekaffeeEntities databaseEntities = new softwarekaffeeEntities();
            var products = databaseEntities.Products.ToList();
            Console.WriteLine("Reading product details.");
            foreach (var data in products)
            {
                Console.WriteLine("Prosuct Id {0} | Product Name {1} | Quantity {2} | Unit Price {3}",
                    data.ProductId,
                    data.ProductName,
                    data.Quantity,
                    data.UnitPrice);
            }
            Console.Read();
        }
    }
}

************************************************************************

Go
create database SoftwareKaffee

Go
use SoftwareKaffee

Go
Create table Products
(
    ProductId int identity primary key,
    ProductName varchar(50),
    ProductDetails nvarchar(200),
    UnitPrice decimal(7,2),
    Quantity int
)

Go
insert into Products values('Dell Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 4GB',55000,4)
insert into Products values( 'HCL Laptop','Processor Intel(RR) Cort(T2) i7 CPU, Ra 8GB',15000,30 )
insert into Products values( 'HP Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 4GB', 23000 ,4 )
insert into Products values( 'HCL Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 4GB',35000,20 )
insert into Products values( 'Linovo Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 2GB',25000,30 )
insert into Products values( 'Copac Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 4GB',30000,30 )
insert into Products values( 'Dell Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 4GB',45000,10 )
insert into Products values( 'HP Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 4GB',55000,30 )
insert into Products values( 'HCL Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 2GB',35000,20 )
insert into Products values( 'Linovo Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 4GB',25000,30 )
insert into Products values( 'Copac Laptop','Processor Intel(R) Cort(T) i5 CPU, Ra 4GB',30000,60 )
insert into Products values( 'Dell Laptop','Processor Intel(R) Cort(T) i3 CPU, Ra 2GB',35000,30 )
insert into Products values( 'HP Laptop','Processor Intel(R) Cort(T) i3 CPU, Ra 2GB',55000,30 )
insert into Products values( 'HCL Laptop','Processor Intel(R) Cort(T) i3 CPU, Ra 2GB',34000,20 )
insert into Products values( 'Linovo Laptop','Processor Intel(R) Cort(T) i3 CPU, Ra 2GB',35000,30 )
insert into Products values( 'Copac Laptop','Processor Intel(R) Cort(T) i3 CPU, Ra 2GB',40000,30 )
insert into Products values( 'Dell Laptop','Processor Intel(RR) Cort(T2) i7 CPU, Ra 8GB',65000,30 )
insert into Products values( 'HP Laptop','Processor Intel(RR) Cort(T2) i7 CPU, Ra 8GB',75000,1 )
insert into Products values( 'HCL Laptop','Processor Intel(RR) Cort(T2) i7 CPU, Ra 8GB',45000,30 )
insert into Products values( 'Linovo Laptop','Processor Intel(RR) Cort(T2) i7 CPU, Ra 8GB',45000,30 )
insert into Products values( 'Copac Laptop','Processor Intel(RR) Cort(T2) i7 CPU, Ra 8GB',37000,5 )


No comments: