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 6093403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:33:20+00:00 2026-05-23T12:33:20+00:00

I have two tables with a Many-To-Many relationship like this: User( emailaddress , Name)

  • 0

I have two tables with a Many-To-Many relationship like this:

User(emailaddress, Name)

UserAlerts(emailaddress, AlertId)

Alert(AlertId,Title)

Alerts have already been added to the database. When inserting a new user, I am doing a lookup on the AlertRepository. The problem is, Instead of creating a record in the User and the UsertAlerts tables only, its also adding an extra Alert record.

I am using the following code:

public ActionResult Register(UserModel model, int[] Alerts)
User user = new MidTier.Models.User();
user.Name = model.Name;
user.EmailAddress = model.EmailAddress;    
if (Alerts!=null)
      {             
         IRepository<Alert> alertRepository = new AlertRepository();
         foreach (int alertId in Alerts)
            {
              Alert alert = alertRepository.First(a=>a.ID== alertId); 
              alertRepository.Detach(alert);  
               if (alert != null)
                  {
                    alert.Enabled = true; 
                      user.Alerts.Add(alert);
                   }                         
             }  

       }
  userRepository.Attach(user);
  userRepository.Add(user);
  userRepository.Save();
  • 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-05-23T12:33:21+00:00Added an answer on May 23, 2026 at 12:33 pm

    Why don’t you try to search little bit before you ask a question? This problem is asked several times per week. In your previous question I said you that you should use same context for loading Alert and storing User. You didn’t do it and complicated whole situation.

    The context doesn’t know anything about existence of the alert. Once you call Add for user it will add all entities which are not tracked yet. There are three ways to solve this:

    • Use the same context in both repositories and do not detach alerts. Because of loading alerts, context will know about their existence and doesn’t insert them again.
    • If you don’t use the same context for loading you must attach the Alert to the new context before you add it to User. That is hard to do when you wrap EF code to repositories.
    • If you don’t use the same context and you will not attach Alert to the new context before you add it to User you must modify your Add method for User and after adding User to the context you must iterate every alert and change its state to Unchanged.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a many to many relationship between two existing tables (User: UserId, Name)
I have two tables with a many to many relationship, through a third table.
I have two tables connected with one to many relationship. Parent Table is a
I have two tables with a linking table describing their many-to-many relationship. TABLE buildings
android noob... I have two tables, with a one to many relationship between country_tbl
I have a many-to-many relationship between two tables, let's say Friends and Foods. If
Is it possible to have a many to many relationship between two tables, and
Possible Duplicate: If I have two tables in SQL with a many-many relationship, do
I hav two models, User and Account, they have a many-to-many relationship through AccountUsers.
I have two tables (Rule and Object) linked through a many-to-many relationship. A Rule

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.