-1

I have a query:

var q = (from c in session.db.students
          where c.id==5
          select c);

How can I delete this record? I've tried deleteonsubmit, deleteobject, delete... but none of them are not known.
For example in code below, delete not known:

foreach (student s in q)
{ s.deleteobject;}
session.db.savechanges();

or:

var q = (from c in session.db.students
          where c.id==5
          select c).deleteOnSubmit();

None of them is not defined.... where is the problem?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ali
  • 98
  • 2
  • 14
  • why did you store you DBContext in session ? And did you try with `db.students.remove(s);` – Emmanuel M. Jul 16 '15 at 08:11
  • @EmmanuelM. i use remove but i see this error: Error 1 The best overloaded method match for 'System.Data.Entity.DbSet.Remove(WindowsFormsApplication1.PurchUtility)' has some invalid arguments – Ali Jul 16 '15 at 09:01
  • can you show us the code that failed using `.Remove` it looks like you not using the right context member or entity Type – Emmanuel M. Jul 16 '15 at 09:17
  • @EmmanuelM. IEnumerable w = (from s in Session.DB.Sponsers where s.Id == 1 select s); foreach(Sponser v in w) { Session.DB.Sponsers.Remove(w); //this line is error } – Ali Jul 16 '15 at 09:52
  • What is `Session.DB`? If it's a type of your own, what's inside the `Remove` method? We can't help without this relevant information. – Gert Arnold Jul 18 '15 at 23:07

5 Answers5

3

You should use Remove:

var q = (from c in session.db.students
          where c.id==5
          select c);

foreach(student s in q)
{
    session.db.students.Remove(s);
}

session.db.SaveChanges();
Jojo
  • 1,875
  • 3
  • 29
  • 29
  • i see this error in line: session.db.students.remove(s) : Error 1 The best overloaded method match for 'System.Data.Entity.DbSet.Remove(WindowsFormsApplication1.PurchUtility)' has some invalid argument – Ali Jul 16 '15 at 08:43
2

You want to call .remove([record]) on the DbContext object. Using your code you would do the following:

var q = (from c in session.db.students where c.id == 5 select c);

foreach (student s in q)
{
    session.db.Remove(s);
}
session.db.SaveChanges();

or using method based querying (to remove a single record with id 5):

var s = session.db.students.Where(p => p.id == 5).FirstOrDefault();
if(s != null) { session.db.Remove(s); }
session.db.SaveChanges();
  • But i dont have Remove too :( IEnumerable w = (from s in Session.DB.Sponsers where s.Id == 1 select s); foreach(Sponser v in w) { Session.DB.Sponsers.Remove(w); //this line is error } – Ali Jul 16 '15 at 08:40
1

Try using dbcontext, you can use remove or removerange.

using (var MyContext= new CRMDBContext(connString))
                {
                    MyContext.[Table].RemoveRange(MyContext.[Table].Where(x => x.[column]== [column]));
                    MyContext.SaveChanges();
                }
halfer
  • 19,824
  • 17
  • 99
  • 186
1

Look this answer. maybe it will help

https://stackoverflow.com/a/17723658/4571664

then also you can use this

 ((DbContext)dbContext).Set<objectName>().Remove(singleObject);
 dbContext.SaveChanges();

if you need multiple object delete, you can use this code in foreach.

Community
  • 1
  • 1
asim.temur
  • 61
  • 6
1

You can use Remove() or RemoveRange() methods on your context for deleting objects.

If you have collection of objects:

session.db.students.RemoveRange(collectionOfStudents);
session.db.SaveChanges();

If you have just one object:

session.db.students.Remove(oneStudent);
session.db.SaveChanges();
Miroslav Holec
  • 3,139
  • 25
  • 22