Wednesday 1 February 2012

Query in Entity Framework


Here is a screenshot of the model created in Visual Studio.


What I did was make a class to complete CRUD operations on the Categories table in the database.  I have a class named Category with methods as needed to accomplish this. I also used lambda expressions in my queries.  

Below I will outline the methods for the following:

Get()
GetByID(int id)
GetByName(string name)
Add(string name)
Update(int id, string newName)
Delete(int id)

Class Outline
?
1
2
3
4
public class Category
{
    ...methods here
}

The variable testDBContext is what allows us to access the entity model so we can run queries and interact with it. The object TestDBEntities was created during the creation of the Entity Model
Get Method
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static List<Categories> Get()
{
    using (TestDBEntities testDBContext = new TestDBEntities())
    {
        try
        {
            return testDBContext.Categories.ToList();
        }
        catch (Exception ex)
        {                   
            throw;
        }
    }
     
}

Get By ID Method
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static Categories GetByID(int id)
{
    using (TestDBEntities testDBContext = new TestDBEntities())
    {
        try
        {
            return testDBContext.Categories.First(cat => cat.Id == id);
        }
        catch (Exception ex)
        {
            throw;
        }
    }
 
}

Get By Name Method
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public static Categories GetByName(string name)
{
    using (TestDBEntities testDBContext = new TestDBEntities())
    {
        try
        {
            return testDBContext.Categories.First(cat => cat.Name.ToLower() == name.ToLower());
        }
        catch (Exception ex)
        {
            throw;
        }
    }
}

Add Method
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public static int Add(string name)
{
    using (TestDBEntities testDBContext = new TestDBEntities())
    {
        try
        {
            Categories cat = new Categories();
            cat.Name = name;
 
            testDBContext.AddToCategories(cat);
            testDBContext.SaveChanges();
 
            return 1;
        }
        catch (Exception ex)
        {
            return 0;
        }
    }
}

Update Method
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static int Update(int id, string newName)
{
    using (TestDBEntities testDBContext = new TestDBEntities())
    {
        try
        {
            Categories catEdit = new Categories();
            catEdit = (from c in testDBContext.Categories where c.Id == id select c).First();
 
            catEdit.Name = newName;
 
            testDBContext.SaveChanges();
 
            return 1;
        }
        catch (Exception ex)
        {
            return 0;
        }
    }
}

Delete Method
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public static int Delete(int id)
{
    using (TestDBEntities testDBContext = new TestDBEntities())
    {
        try
        {
            Categories catEdit = new Categories();
            catEdit = (from c in testDBContext.Categories where c.Id == id select c).First();
 
            testDBContext.DeleteObject(catEdit);
            testDBContext.SaveChanges();
 
            return 1;
        }
        catch (Exception ex)
        {
            return 0;
        }
    }
}

Also here is some quick debug code if you would like to put in your code behind of your ASPX page.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
protected void Page_PreRender(object sender, EventArgs e)
{
    //Get Categories
    List<Categories> lst = Category.Get();
    lst.ForEach(delegate(Categories cat)
    {
        Response.Write("<strong>List: </strong>" + cat.Name + "<br />");
    });
 
    //Get Category By ID
    Response.Write("<strong>By ID: </strong>" + Category.GetByID(2).Name + "<br />");
 
 
    //Get Category By Name
    Response.Write("<strong>By Name: </strong>" + Category.GetByName("Football").Name + "<br />");
 
    //Add Category
    //Response.Write("Result of Add Category: " + Category.Add("New Category").ToString());
 
    //Update Category
    //Response.Write("Result of Update Category: " + Category.Update(11, "Updated Category").ToString());
 
    //Delete Category
    //Response.Write("Result of Delete Category: " + Category.Delete(11).ToString());
 
}

Well I hope this will get you started like it did for me. I look forward to working more with the framework and getting into more complex queries.
As usual with me, I'm a concept driven developer so I need to understand the concept of something and I explore from there.

No comments:

Post a Comment