LINQ to Entities delete all entries in many-to-many relationship?

If you don't like executing a store command like Paperjam suggested you could also create a stored procedure for this. That would nicely map to a static typed function on your ObjectContext.

Up vote 1 down vote favorite share g+ share fb share tw.

I asked a very similar question earlier today, so you might get a bit of deja vu, but I'm afraid I can't work this one out. I have 3 MySql tables: Students, Classes and StudentsInClasses. The Entity Framework translates these into two entities Student and Class, each linking to the other with a many-to-many navigation property (e.g. Student.

Classes). But there is no StudentsInClasses entity, so what's the best way to call, using LINQ to Entities, the equivalent of SQL: DELETE FROM StudentsInClasses; I definitely want to avoid loading all Classes and their Students. Of course I could do it that way, but that would be horrendous because there are thousands of them and there should be no need.

Many thanks. .net linq entity-framework linq-to-entities link|improve this question asked Oct 18 '11 at 1:00James1,0151317 96% accept rate.

First: As far as I know, you can't delete data using EF without first loading that data. Secondly: you can use ADO or POCO (above EF.4): try { using (testEntities db = new testEntities()) { db. ExecuteStoreCommand("DELETE FROM StudentsInClasses;", NULL); } } catch (Exception ex) { _Exceptions.

ManageExceptions(ex);}.

If you have a doubt on your EF mapping, you can check the metadata file. These file are usually present in the directory obj/Debug/edmxResourcesToEmbed of your project. The metadata file yourEntities.

Msl defines the mapping between objects and tables. In this file you should find that the Students. Classes is mapped on the StudentInClasses table, so removing entries in that list and saving the Students is equal to delete rows on the StudentInClasses table.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions