473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Linq to EF (EF4) deleting entry from many-to-many joining table

Entities:

Article
ArticleID

ImageMaster
ImageID
ImageType (1, 2, 3, or 4)

ImageArticle <-- this entity is produced by EF because of extra fields
ImageID
ArticleID
Title
Desc
Alt

I'm trying to delete all entries in ImageArticle where ArticleID=### and ImageType=4

I tried
Expand|Select|Wrap|Line Numbers
  1. foreach (ImageArticle ia in Article.ImageArticles.Where(i => i.ImageMaster.TypeID == 4))
  2. {
  3.    context.ImageArticles.Delete(ia);
  4. }
  5. context.SaveChanges();
  6.  
But it gives me an error about foreign keys not being nullable.

I also tried Article.ImageArticles.Remove(ia), but then it wont let me iterate through the loop because the collection changed inside the loop.

The next thing I tried was gathering all the ImageIDs that I need to delete, then do this:

Expand|Select|Wrap|Line Numbers
  1. context.ExecuteStoreCommand("DELETE FROM ImageArticle WHERE ArticleID = " + Article.ArticleID + " AND ImageID in (" + IDs + ")");
  2. context.SaveChanges();
  3.  
But that also gave me the foreign key error.

How can I achieve deleting these rows in EF?
Oct 13 '11 #1
1 2083
follow up:

ImageArticle
ImageID pk/fk (not nullable)
ArticleID pk/fk (not nullable)
Title
Desc
Alt
Oct 14 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Michael Onfrek | last post by:
Hi, is copy, paste, cut of selection possible in entry widget? Docs say selection must be copied by default, in my programm it doesn't work. Regards, M.O.
15
by: Rick | last post by:
Hi, Does deleting an object more than one times incur undefined behavior? I think it doesn't but just making sure... thanks Rick
1
by: Lucky | last post by:
uninstall service using installUtil /u says it was succesfull, but the entry is still shown in the services. Tried also deleting from HKLM\SYSTEM\CurrentControlSet\Services\.., makes no difference....
2
by: Masoud Sharify | last post by:
does vc#2005 supports LINQ?(query in C# code)
3
by: jack | last post by:
Iv just heard of LINQ what is this all about
1
by: Chris | last post by:
I sort of asked this in my previous post but what is the status of linq? Is it in .net v3? Regards, Chris.
3
gauravgmbhr
by: gauravgmbhr | last post by:
hi friends I have been doing testing on an existing project The tables in postgresQl Db has some trigger function using keyword 'DELETING' IF DELETING THEN SOME SELECT STATEMENT...
22
by: paululvinius | last post by:
Hi! Testing som Linq-expressions and tried to measure performance and compare it to pre-Linq programming. The folloing two methods are functional equal but the non-Linq one is twice as fast....
0
by: Deepak.Verma3441 | last post by:
LINQ Book Introducing Microsoft LINQ http://jumbooks.com/books/Computers%20and%20Internet/Introducing%20Microsoft%20Linq/
0
by: harshil | last post by:
Hi, Actually I am facing problem in deleting selected checkbox row..I mean for username & password.When I click on its check box and press delete than the entry for that username & password in Ms...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.