May, 2010

  • Wriju's BLOG

    ADO.NET Entity Framework 4.0 : POCO – The Code Only Approach

    • 2 Comments

    POCO in ADO.NET Entity Framework 4.0 has been discussed in many places. Here I am not going to talk about it. However, I created a small sample to demonstrate the capability of POCO in much more cleaner “code-only” approach.

    <<Apology for this blog post issue>>. There were some images, due to which it was not showing up. I will add them later.

    I am using a small test database with two tables,

    Dept

    ====

    DeptId

    DeptName

    EmpDept

    =======

    EmpId

    EmpName

    DeptId FK

    I have created one ADO.NET Entity Framework Model

    Now, to have POCO in-place I removed the Custom Tool property of the model to blank from “EntityModelCodeGenerator

    Now, wrote the below code.

     

    //Class Dept

    public class Dept

    {

        public Dept()

        {

            EmpDepts = new List<EmpDept>();

        }

     

        public int DeptId { get; set; }

        public string DeptName { get; set; }

       

        //Navigation Property of EmpDepts

        public virtual List<EmpDept> EmpDepts { get; private set; }

    }

     

    //EmpDept class

    public class EmpDept

    {       

        public int EmpId { get; set; }

        public string EmpName { get; set; }

     

        //Navigation Property of EmpDepts

        public Dept Dept { get; set; }

    }

     

    public class TestDBEntities : ObjectContext

    {

        //Base class constructor initializes with connection string

        public TestDBEntities()

            : base("name=TestDBEntities")

        {

            ContextOptions.LazyLoadingEnabled = true;

        }

     

     

        public IObjectSet<EmpDept> EmpDepts

        {

            get { return CreateObjectSet<EmpDept>(); }

        }

     

        public IObjectSet<Dept> Depts

        {

            get { return CreateObjectSet<Dept>(); }

        }       

    }

     

    Now in the main method it supports Lazy Loading as well.

     

    static void Main(string[] args)

    {

        using (var ctx = new TestDBEntities())

        {

            var q = from c in ctx.Depts

                    select c;

     

            foreach (var k in q)

            {

                Console.WriteLine(k.DeptName);

                Console.WriteLine(k.EmpDepts.Count);

            }

        }

    }

     

    Namoskar!!!

  • Wriju's BLOG

    LINQ to SQL : Windows Forms Dynamic Where Clause

    • 0 Comments

    While I was reading the book LINQ in Action, I found this chapter. Below is how we can do in LINQ to SQL. The scenario is if you want to create a LINQ and pass the where clause based on user input from UI, you need to write different queries. Instead, how you can do it in single LINQ. I have used the Pubs from SQL Server 2000 sample from here.

     

    Below are the steps done,

     

    1.    Open Visual Studio 2010

    2.    Create new project from File > New Project > Windows Forms Application, name it LINQ_Custom_Search

    3.    Add combo box with name comboBox1

    4.    Choose the property of comboBox1 and add Title Asc, Title Desc, Price Asc, Price Desc

    5.    Add button with a name btnSort, text Sort

    6.    Add a datagrid view with a name dataGridView1

    7.    Select the project from Solution Explorer, right click and Add > New Item.

    8.    Select LINQ to SQL Classes, name it pubsDB.dbml

    9.    From Server Explorer drag the table Title from Pubs database and drop it to pubsDB.dbml.

    10. Add one utility method

     

    void CustomSort<TKey>(Func<title, TKey> selector, Boolean ascending)

    {

        IEnumerable<title> titles = db.titles;

        titles = ascending ? titles.OrderBy(selector) : titles.OrderByDescending(selector);

        dataGridView1.DataSource = titles.ToList();

    }

     

    11. Add the below code to btnSort’s click event

     

    private void btnSort_Click(object sender, EventArgs e)

    {

        switch (comboBox1.SelectedIndex)

        {

            case 0:                   

                CustomSort(title => title.title1,true);

                break;               

            case 1:

                CustomSort(title => title.title1, false);

                break;               

            case 2:

                CustomSort(title => title.price,true);

                break;               

            case 3:

                CustomSort(title => title.price, false);

                break;

        }

    }

     

    12. And finally the simple code

    pubDBDataContext db = new pubDBDataContext();

           

    private void Form1_Load(object sender, EventArgs e)

    {

        dataGridView1.DataSource = db.titles.ToList();

    }

     

    And you are done. Now people can choose the sort criteria from the application and based on that selection the query will be populated and generated.

     

     

    Namoskar!!!

  • Wriju's BLOG

    Visual Studio 2010 : Dependency Graph, Sequence Diagram and Architecture Explorer

    • 0 Comments

    I generally avoid meta blogging, but while going through my MSDN RSS feed I found one very detailed article on few of my favorite Visual Studio 2010 Features at

    http://blogs.msdn.com/jennifer/archive/2010/05/11/visual-studio-2010-how-to-understand-your-code-using-dependency-graphs-sequence-diagrams-and-the-architecture-explorer.aspx

    It’s a good read indeed.

    Namoskar!!!

Page 1 of 1 (3 items)