1

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

Also I've done Get all records by using this method in my repo class,

    StudentManagementEntities _db;
    public Repo()
    {
        _db = new StudentManagementEntities();
    }

    public object GetAllStudents()
    {
        return _db.People.Select(s => s).ToList();
    }

I don't know how to do other operations like Insert, Update, delete etc.,

would somebody tell me the linq for that or else give me any examples link...

3
  • Brotip: That .Select(s => s) does absolutely nothing useful and you can remove it. Commented Dec 31, 2012 at 12:19
  • Just a tiny note for correctness, maybe you want GetAllStudents() to return IEnumerable<Student>, not object Commented Dec 31, 2012 at 12:23
  • 1
    Btw, a bit of a zen koan. I know all Students are People, but are all People Students? Commented Dec 31, 2012 at 12:28

1 Answer 1

4

Add, Update and Delete Objects in Entity Framework 4.0

// Insert

public void AddStudent(People s)
{
    _db.People.Add(s);
    _db.SaveChanges();
}

// Delete

public void DeleteStudent(People s)
{
    _db.People.Remove(s);
    _db.SaveChanges();
}

//Edit

public void EditStudent(People s)
{
    var people = _db.People.First( p=> p.ID == s.ID); // Replace ID with primary key

  // Copy all properties from s to people

    _db.SaveChanges();
}
Sign up to request clarification or add additional context in comments.

2 Comments

... and _db.SaveChanges()
Also depending on the type of context the method to add things might be AddObject. Personally I prefer the code first approach which lets you use more familiar methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.