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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:47:24+00:00 2026-06-18T15:47:24+00:00

I have the following code: var roleEditionCNT = new Role { ApplicationName = TTT,

  • 0

I have the following code:

            var roleEditionCNT = new Role { ApplicationName = "TTT", RoleName = "EDITION_CNT" };
            var roleEditionKCD = new Role { ApplicationName = "TTT", RoleName = "EDITION_KCD" };
            var roleEditionInterne = new Role { ApplicationName = "TTT", RoleName = "EDITION_INTERNE" };
            var roleConsultationInterne = new Role { ApplicationName = "TTT", RoleName = "CONSULTATION_INTERNE" };
            var roleConsultationExterne = new Role { ApplicationName = "TTT", RoleName = "CONSULTATION_EXTERNE" };
            var roleAdministrator = new Role { ApplicationName = "TTT", RoleName = "ADMINISTRATOR" };

            new List<Role>
                {
                    roleEditionCNT,
                    roleEditionKCD,
                    roleEditionInterne,
                    roleConsultationInterne,
                    roleConsultationExterne,
                    roleAdministrator
                }.ForEach(b => context.Roles.AddOrUpdate(m => m.RoleName, b));

Then I can do:

var myUser = new User
      {
        Company = "DELTOY",
        FirstName = "BRYAN",
        LastName = "KENETH",
        Roles = new List<Role> { roleEditionCNT }
      };

Now I would like to modify the way I assign roles like this:

var myUser = new User
      {
        Company = "DELTOY",
        FirstName = "BRYAN",
        LastName = "KENETH",
        Roles = new List<Role> { new Role { ApplicationName = "TTT", RoleName = "EDITION_CNT" } }
      };

This code compiled successfully but at runtime I got the error:

Violation of PRIMARY KEY constraint ‘PK_Roles’. Cannot insert
duplicate key in object ‘dbo.Roles’

By simply modifying the above code, it seems the system is trying to add a new record to the table Roles. How can I simply assign Role but not using the variable. I prefer to give rolename with a string.

Any idea?

  • 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-18T15:47:25+00:00Added an answer on June 18, 2026 at 3:47 pm

    In the second example you are trying to add a User with a new Role as opposed to assigning it to an existing Role. The Role you are trying to add already exists in the Roles table.

    You can get the Role object into a variable like.

    var role = context.First(p => p.RoleName == "EDITION_CNT");
    

    and then assign it to the new User.

    var myUser = new User
          {
            Company = "DELTOY",
            FirstName = "BRYAN",
            LastName = "KENETH",
            Roles = new List<Role> { role } 
          };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: var carsContext = new CarsDataContext(); IQueryable<Car> allCars = carsContext.Cars;
I have the following code: var tlp = new TableLayoutPanel { Location = new
I have the following code: var myRootRef = new Firebase('https://myacct.firebaseIO.com'); var collectionRef = myRootRef.child('collection');
I have the following code: var service = new Mock<INavigationService>(); service.Setup(x => x.GetSchemes(new SchemeFilterEntity())).Returns(new
I have the following code: var psi = new ProcessStartInfo(); psi.FileName = @C:\gw\gw.exe; psi.WorkingDirectory=@C:\gw;
I have the following code: var intrebari = new Array(); var i = 0;
I have the following code: var numberOfSelectedOptions = new Array(); numberOfSelectedOptions[0]=0; numberOfSelectedOptions[1]=0; numberOfSelectedOptions[2]=0; $(a.tagvariantoption).live(click,
I have the following code: var words = new Object(); $(li.words).each(function(){ var thisId =
I have the following code var section = new CustomConfigurationSection(); section.SectionInformation.Type = System.Configuration.NameValueFileSectionHandler; section.SectionInformation.SetRawXml(sectionXml);
I have the following code: var tf:TextFormat = new TextFormat(); tf.font = arial; aRButton.textField.antiAliasType

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.