Tuesday, 30 October 2012

entity framework enum support | Enum Support in EF5

1)  Add new item to the Project "ADO.NET Entity Framework"
2) Name it as Projects.edmx
3) Add Properties to Project entity as shown below

5) Build the Project
6) Add Enum Entity to edmx file
            7)  Link Enum type to  Status Property as shown in the Enity
8)Build the Project

Model First in Entity Framework 5

Model First in Entity Framework 5


Step1)  Create a Console Project
Step 2) Add  New Item  --> select "ÄDO.NET Entity Framework" and name it as Hotel.edmx
   
Step 3)   Select Empty model , because you are going to generate SQL Tables based on Entities(Model First) you are going to create in step 4

Step 4) Add Entity from Toolbox  as shown below, Name it as Hotels

Step 5)   Right click on Edmx file
                  select Generate Database from Model... option

 Step 7)  Enter Database details with username & password/select windows authentication
              Select Existing database or Create new database using sqlcmd/Sql Management Studio.
  Note: Database name should exist.




Step 8)  Choose your database connection


Step 9)  Generate Database wizard with ur entity DDL commands
as shown below

Step 10)  Build the Project
Step 11)  Execute Hotel.edmx.sql  
Step 12)  Now Goto Sql server  and check DB Table corresponding to Hotel Entity created on selected database(Step 7)

Step 13) Open Hotel.Context.cs file
                You will find   DBcontext derived class with  HotelContainer

Step 14)  Open Program.cs file
                  Add following code

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {

            HotelContainer context1 = new HotelContainer();
            context1.Hotels.Add(new Hotels
            {
                 HotelName="mariott hotels",
                  Address="450 north mathilda",
                  City="Sunnyvale",
                   State="California",
                    ZipCode="94086",
                     Country="USA"
            });

            context1.SaveChanges();//save changes to database

            //Get hotel details from Database
            foreach (Hotels hotel in context1.Hotels)
            {
                Console.WriteLine(hotel.HotelName+"\t"+hotel.City+"\t"+hotel.Country);
            }

        }


Step 15) Output