October, 2008

  • Wriju's BLOG

    ADO.NET Entity: Insert Update and Delete with Relationship

    • 18 Comments

    Few days back I had written an article on Insert/Update/Delete for simple standalone tables at ADO.NET Entity: Insert Update and Delete. Now after that many of you had requested me to put article on how it works with relationship.

     

    Here I will use a database created by me. There will be two tables connected with each other.

    image

    Now I will create TestDB.edmx out of this database.

     

    image 

    Insert

     

    using (TestDBEntities ctx = new TestDBEntities())

    {

        //Create new Department

        Dept d = new Dept() { DeptName = "ADO Entity" };

     

        //Create new Employee 1

        EmpDept ed1 = new EmpDept() { EmpName = "ADO Employee 1" };

     

        //Create new Employee 2

        EmpDept ed2 = new EmpDept() { EmpName = "ADO Employee 2" };

     

        //Add employee to the Dept *OBJECT*

        d.EmpDept.Add(ed1);

        d.EmpDept.Add(ed2);

     

        //Updating the context

        ctx.AddToDept(d);

     

        //Save to Database

        ctx.SaveChanges();

    }

     

    Update

     

    using (TestDBEntities ctx = new TestDBEntities())

    {

        //Get an existing Department

        Dept dep = (from d in ctx.Dept

                    where d.DeptId == 22

                    select d).First();

     

        //Set new Department name

        dep.DeptName = "ADO.NET 3.0";

     

        //Create new Employee 2

        EmpDept ed2 = new EmpDept() { EmpName = "ADO 2" };

     

        //Add *new* employee to the Dept *OBJECT*

        dep.EmpDept.Add(ed2);

     

        //Save to Database

        ctx.SaveChanges();

    }

     

    Delete

     

    using (TestDBEntities ctx = new TestDBEntities())

    {

        //Get an existing Department

        Dept dep = (from d in ctx.Dept.Include("EmpDept")

                    where d.DeptId == 22

                    select d).First();

     

        /*

         Needd to do ToList() becuase once you delete

         a record then iteration will not be possible.         

        */

        foreach (EmpDept ed in dep.EmpDept.ToList())

        {

            //This removes relationship from Context

            dep.EmpDept.Remove(ed);

     

            //Delete it from context

            ctx.DeleteObject(ed);       

        }

     

        //Delete the master table

        ctx.DeleteObject(dep);

           

        //Save to Database

        ctx.SaveChanges();

    }

     

    Note: during delete you first need to remove the relationship from entity and then delete the object from entity. So you need to keep the child data offline and then do operation. Then delete the main object.

     

    In my next post I will write about “how to select with Relationship”.

     

    Namoskar!!!

     

  • Wriju's BLOG

    ADO.NET Entity: LINQ to Entity with Relationship

    • 4 Comments

    Relationship and getting data from Entity Framework using Northwind database

    image

    Simple Query

     

    using (NorthwindEntities ctx = new NorthwindEntities())

    {

        var query = from o in ctx.Orders

                    select o.OrderID;

     

        foreach (var k in query)

        {

            Console.WriteLine(k);

        }

    }

     

    Now if you want to filter this with the parent information,

     

    var query = from o in ctx.Orders

                where o.Customers.City == "London"

                select o.OrderID;

    This actually goes one level up and then filters the data.

     

    Include Child Information

     

    var query = from c in ctx.Customers

                where c.City == "London"

                select c;

     

    foreach (var k in query)

    {

    Console.WriteLine(k.ContactName);

    }

     

    Now if you want to get the “Orders” table, there are two ways to do it,

     

    Immediate Loading

     

    var query = from c in ctx.Customers.Include("Orders")

                where c.City == "London"

                select c;

     

    foreach (var k in query)

    {

        Console.WriteLine(k.Orders.Count);

    }

     

    On-Demand Loading

     

    var query = from c in ctx.Customers

                where c.City == "London"

                select c;

     

    foreach (var k in query)

    {

        //if not loaded then load it

        if (!k.Orders.IsLoaded)

            k.Orders.Load();

     

        Console.WriteLine(k.Orders.Count);

    }

     

     

    Namoskar!!!

     

  • Wriju's BLOG

    Application Architecture Guide 2.0 EBook

    • 4 Comments

    For you to design the better future.

    image

    Link to download http://www.codeplex.com/AppArchGuide 

    Namoskar!!!

  • Wriju's BLOG

    Visual Studio 2010: First Look of my VS 2010

    • 3 Comments

    How this new Visual Studio 2010 looks like? This has three views. Here it is,

    Start Page > Welcome

    image

    Start Page > Projects

    image

    Start Page > Visual Studio

    image

    Namoskar!!!

  • Wriju's BLOG

    C# 4.0: My First Dynamic Program

    • 2 Comments

    I am super excited to see Anders demonstrating C# 4.0 for first time in public. I am a real fan of him and downloaded the public bit to tryout. Here is my first Dynamic C# 4.0 program.

     

    There is nothing much to be excited from my Program but I am very much after hitting F5.

     

    static void Main(string[] args)

    {

        dynamic i = 10;

        dynamic sHello = "Hello C# 4.0";

     

        Console.WriteLine(i);

        Console.WriteLine(sHello);

    }

     

    More to come.

     

    Namoskar!!!

  • Wriju's BLOG

    PDC 2008 Must be Enjoyed

    • 1 Comments

    If you are thinking that you are a looser not by attending PDC 2008, forget it. Stretch your body as you might to stay connected to your chair for long time.

    What is

    • Visual Studio 2010
    • Windows Azure
    • Project "Velocity"
    • Dublin
    • Oslo
    • Parallelism
    • S+S

    and many more.. So without wasting time join and view the video, download them.

    https://sessions.microsoftpdc.com/

    https://sessions.microsoftpdc.com/public/timeline.aspx

    image

    Namoskar!!!

  • Wriju's BLOG

    MSDN Magazine Online

    • 1 Comments

    Get your MSDN magazine from web at http://msdn.microsoft.com/msdnmag/.

    Namoskar!!!

  • Wriju's BLOG

    Visual Studio 2010 and .NET Framework 4.0 Overview

    • 0 Comments

    Know more about it and keep you sink with http://msdn.microsoft.com/en-us/vstudio/products/cc948977.aspx

    What's New

    Additional Resources

    Namoskar!!!

Page 1 of 1 (8 items)