How to write LINQ queries for CRUD using Entity Framework?

I am using EntityFramework (EF V6) with Asp.Net to create a single website. In this, I created .edmx and .tt and DBContext.

I am trying to create objects for each table so that I can call it later using aspx

I do not know if I am writing my LINQ queries correctly! Why do I need your help.

Table. I am trying to set a LINQ object for it:

enter image description here

I created this class of objects:

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(); } } 

Right or wrong?

+5
source share
2 answers

These are the only methods that 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. //ieOldsub.Name = Newsub.Name; ctx.SaveChanges(); } } 
+3
source

I would recommend that you change the logic to a new context in the used block instead of accessing the general context. EF Contextual resource:

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

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

  • When working with long-term contexts, consider the following:
    • As more objects and their references are loaded into memory, context consumption can increase rapidly. This can cause performance issues.
    • Remember to get rid of the context when it is no longer required.
    • If the exception causes the context to be in an unrecoverable state, the entire application may terminate.
    • The chances of running into concurrency problems widen as the gap between the times when data is requested and updated.
  • When working with web applications, use a context instance for each request.
  • For Windows Presentation Foundation (WPF) or Windows Forms, use a context instance for each form. This allows you to use the change tracking features provided by the context.

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

+1
source

All Articles