Sorry that should have read:
Main Topics
Browse All TopicsI have created a simple blog which I am having some issues with. It saves OK, but updating causes an error:
"Server Error in '/' Application.
Violation of PRIMARY KEY constraint 'PK_Blog_CategoryMapping'.
I think this is because its trying to insert the same keys again instead of updating the keys.
Please see capture.png for a screen grab of my linq ORM.
See my code snip below for saving.
I think the problem is : be.BlogCategoryMappings.Ad
What I need to do is delete / update the mappings in CategoryMappings table, not try and insert them again. How is this done?
Thanks in advance
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hi iziizi;
If the bcm.BlogID and bcm.CategoryID combination already exist in the BlogCategoryMapping table then do not attempt to added it again. These entries already exist so you must be modifying it. Once the records are in the tables there primary key will not change even when you edit them.
So this code should be done when you are mapping new entries from the tables and Not editing entries in the tables.
foreach (var i in bcml)
{
BlogCategoryMapping bcm = new BlogCategoryMapping();
bcm.BlogID = i.BlogID;
bcm.CategoryID = i.CategoryID;
be.BlogCategoryMappings.Ad
}
To your question, "What I need to do is delete / update the mappings in CategoryMappings table, not try and insert them again. How is this done?", If the records are already in the tables, BlogEntry and BlogCategory, and are already a member of the BlogCategoryMapping table then to modify the records in the tables BlogEntry or BlogCategory get a copy of the record from the database through your DataContext, change/modify the fields that need to be change and then do a db.SubmitChanges();. That is all that is needed to be done in an edit.
Fernando
Hi Fernando,
Thanks for your reply.
"If the bcm.BlogID and bcm.CategoryID combination already exist in the BlogCategoryMapping table then do not attempt to added it again. These entries already exist so you must be modifying it. Once the records are in the tables there primary key will not change even when you edit them."
What happens if I want to remove a mapping? Do i not need to instruct it to be deleted?
Hi iziizi:;
To your question, "What happens if I want to remove a mapping? Do i not need to instruct it to be deleted?", Yes you do need to issue a command to delete an entry from the BlogCategoryMapping table. You will need both the BlogCategory and BlogEntry primary keys to do so because the entry in the BlogCategoryMapping table primary key is made up from both of these keys. See code snippet for a way to do this. Note that this will not delete any of the entries in the other two tables but just the BlogCategoryMapping table.
In the question title, "LINQ to SQL - 1 to many update / insert" The relationship between BlogCategory to BlogEntry is a Many to Many with the BlogCategoryMapping table being what is known as the junction table. So to insert records and do the mapping both BlogCategory and BlogEntry must exist before inserting into the BlogCategoryMapping table.
Fernando
Business Accounts
Answer for Membership
by: iziiziPosted on 2009-07-24 at 08:11:27ID: 24935693
update:
If i change it so i first delete the mappings on an update as so:
if (BlogID > 0)
{
be = db.BlogEntries.Where(b => b.BlogID == BlogID).FirstOrDefault();
}
else
{
be = new BlogEntry();
IsNew = true;
}
Then it works. However this doesnt seem like the correct way of dealing with mappings. Can someone please shed some light?