Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8137825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:17:20+00:00 2026-06-06T11:17:20+00:00

I have many to many relationship in entity framework. See image… I want to

  • 0

I have many to many relationship in entity framework. See image…
many to many

I want to insert an article and tags for this article, but TagName property is unique in db and so I need only new tags to be inserted in Tags and in ArticlesToTags(not seen in EF) tables while others that are already in Tags need to be inserted only in ArticlesToTags.

public void CreateUpdate(string title, string subTitle, string text,
                string author, string tags, string photo, bool allowComments)
            {
                using (var context = new blogEntities())
                {
                        var article = new Article()
                        {
                            Title = title,
                            SubTitle = subTitle,
                            ArticleText = text,
                            Author = author,
                            Photo = photo,
                            CreateDate = DateTime.Now,
                            ModifyDate = null,
                            AllowComments = allowComments
                        };

                        foreach (var tg in tags.Split(','))
                        {
                            article.Tags.Add(new Tag() { TagName = tg });
                        }

                        context.Articles.AddObject(article);

                    context.SaveChanges();
                }
            }

Now it throws exception of duplicate unique key cannot be inserted How is it done in EF? I’m new to it…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T11:17:23+00:00Added an answer on June 6, 2026 at 11:17 am

    Add a select statement to see if there is already a tag in the database.

    foreach (var tg in tags.Split(','))
    {
        var tag = context.Tags.SingleOrDefault(x => x.TagName == tg);
        if (tag == null)
            tag = new Tag() { TagName = tg };
    
        article.Tags.Add(tag);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: my revised Entity relationship diagram A Student can have many contact times but
I have a JPA object which has a many-to-many relationship like this: @Entity public
I have a many-to-many relationship between 2 entities in Entity Framework, like here .
I am using Entity Framework 4. I have a many-to-many association (relationship) between two
I have a many-to-many relationship in my Entity Framework 4 model (which works with
I use Entity Framework, and I have a many-to-many relationship in the database between
I have a Tag and an Article object with a many-to-many relationship using Entity
I am using the Entity Framework. I have a many to many relationship Articles
I have 2 Entity Framework entity sets with many-to-many relationship (compromised of 3 DB
I have two Entity Framework objects with a one-to-many relationship: widget(parent) and widgetNail(child) Widget

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.