Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write LINQ Queries for CRUD using Entity Framework? [closed]

I'm using EntityFramework(EF V6) with Asp.Net for creating one website, In that I've Created the .edmx and .tt and DBContext.

I'm trying to create an objects for each table to summoner it later with aspx

I don't know if I'm writing my LINQ Queries in the right way!, that's why I need your help on this.

The Table i'm trying to establish an LINQ object for it in this picture:

enter image description here

This Object Class I have created:

public class LINQSubjects 
{
    NewsPaperEntities ctx = new NewsPaperEntities();
    // Get Subject
    public Subject GetSubject(int SubjectID)
    {
        Subject sub = ctx.Subjects.FirstOrDefault(s=> s.Subject_ID==SubjectID);
        return sub;
    }
    // Get All Subject Info
    public List<Subject> GetAllSubjects()
    {
        List<Subject> sublist = (from s in ctx.Subjects select s).ToList<Subject>();
        return sublist;
    }
    // Insert a Subject
    public void AddSubject(Subject Addsub)
    {
        ctx.Subjects.Add(Addsub);
        ctx.SaveChanges();
    }
    // Delete a Subject
    public void DeleteSubject(int SubjectID)
    {
        Subject sub = ctx.Subjects.FirstOrDefault(s => s.Subject_ID == SubjectID);
        ctx.Subjects.Remove(sub);
        ctx.SaveChanges();
    }
    // Edit a Subject
    public void UpdateSubject(Subject Newsub)
    {
        Subject Oldsub = ctx.Subjects.FirstOrDefault(s => s.Subject_ID == Newsub.Subject_ID);
        Oldsub = Newsub;

        ctx.SaveChanges();
    }
}

is it right or wrong?

like image 706
Ameen Chaabani Avatar asked Sep 27 '22 22:09

Ameen Chaabani


2 Answers

These are the only methods I would change, the rest look right.

Updated:

    public List<Subject> GetAllSubjects()
    {
        List<Subject> sublist = ctx.Subjects.ToList();
        return sublist;
    }
    public void DeleteSubject(int SubjectID)
    {
        Subject sub = ctx.Subjects.FirstOrDefault(s => s.Subject_ID == SubjectID);

        if(sub!=null)//FirstorDefault can return null
        {
           ctx.Subjects.Remove(sub);
           ctx.SaveChanges();
        }
    }
    //This is with the assumption that the parameter Newsub is attached to the context already. 
    //As in you got the sub from the context then changed it then passed it into UpdateSubject
    public void UpdateSubject(Subject Newsub)
    {
        Subject Oldsub = ctx.Subjects.FirstOrDefault(s => s.Subject_ID == Newsub.Subject_ID);
        if(Oldsub !=null)//FirstorDefault can return null
        {                
            Oldsub = Newsub;
            //If Newsub is not attached you have to set manually set each property.
            //i.e.Oldsub.Name = Newsub.Name;
            ctx.SaveChanges();
        }
    }
like image 168
imGreg Avatar answered Nov 15 '22 07:11

imGreg


I would recommend that you change your logic to new a context in a using block instead of referring to a shared context. EF Context's life:

using(var ctx = new NewsPaperEntities())
{
    ctx.Subjects.Add(Addsub);
    ctx.SaveChanges();
}

Here are some general guidelines when deciding on the lifetime of the context:

  • When working with long-running context consider the following:
    • As you load more objects and their references into memory, the memory consumption of the context may increase rapidly. This may cause performance issues.
    • Remember to dispose of the context when it is no longer required.
    • If an exception causes the context to be in an unrecoverable state, the whole application may terminate.
    • The chances of running into concurrency-related issues increase as the gap between the time when the data is queried and updated grows.
  • When working with Web applications, use a context instance per request.
  • When working with Windows Presentation Foundation (WPF) or Windows Forms, use a context instance per form. This lets you use change-tracking functionality that context provides.

reference: https://msdn.microsoft.com/en-us/data/jj729737.aspx?f=255&MSPPError=-2147217396

like image 41
Gent Avatar answered Nov 15 '22 07:11

Gent