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

  • Home
  • SEARCH
  • 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 9241183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:19:31+00:00 2026-06-18T08:19:31+00:00

I have a third party service from which I’m getting a large collection of

  • 0

I have a third party service from which I’m getting a large collection of data and I want to store that information in my local database. Suppose I have 3 tables in my database User, Role and UserRole.

  1. User table contains the following fields – UserId(PK), UserName.
  2. Role table contains the following fields – RoleId(PK), RoleName.
  3. UserRole table contains the following fields – UserId(FK),RoleId(FK).

So there is a Many To Many relationship between User and Role table.

When I try to save the following records to database its generating duplicate records in Role table.

using (CFAContext context = new CFAContext())
{
    context.Users.Add(new User { UserName = "User 1", Roles = new List<Role> { new Role { RoleName = "Role 1" }, new Role { RoleName = "Role 2" }, new Role { RoleName = "Role 3" } } });
    context.Users.Add(new User { UserName = "User 2", Roles = new List<Role> { new Role { RoleName = "Role 1" }, new Role { RoleName = "Role 2" } } });
    context.Users.Add(new User { UserName = "User 3", Roles = new List<Role> { new Role { RoleName = "Role 1" }, new Role { RoleName = "Role 4" } } });
    context.SaveChanges();
}

How can I insert this list to database(User and Role) without any duplicate records in User and Role table ?

  • 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-18T08:19:32+00:00Added an answer on June 18, 2026 at 8:19 am

    I guess the RoleId fields are unique identifiers, thus for each role you have added, you will get a new record in the database with different RoleId fields as you are creating a new Role instance.

    If you change your code to the following it will work;

    Role role1 = new Role { RoleName = "Role 1" }
    Role role2 = new Role { RoleName = "Role 2" }
    Role role3 = new Role { RoleName = "Role 3" }
    Role role4 = new Role { RoleName = "Role 4" }
    
    context.Users.Add(new User { UserName = "User 1", Roles = new List<Role> { role1, role2, role3 } });
        context.Users.Add(new User { UserName = "User 2", Roles = new List<Role> { role1,role2 } });
        context.Users.Add(new User { UserName = "User 3", Roles = new List<Role> { role1, role4 } });
        context.SaveChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a Windows Service which listens for data from a third party
I have third-party C code that writes data to a file. I want to
I have a PHP script that syncs data with a third party service, and
I have a third party web service that returns this xml <book> <release_date>0000-00-00</release_date> </book>
I have a form that submits to a third-party service that runs some validation.
I have a web page (.aspx) that calls a third party web service to
I have a simple web service that has an API third party developers are
I have an ASP.NET application which talks to a third-party SOAP web service. My
I have an application built that hits a third party company's web service in
I have a web application which fetches information from a web service. It works

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.