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:
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?
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();
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With