Assigning a role by using a string >> Violation of PRIMARY KEY constraint
<p>I have the following code:</p>

<pre><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&lt;Role&gt;
                {
                    roleEditionCNT,
                    roleEditionKCD,
                    roleEditionInterne,
                    roleConsultationInterne,
                    roleConsultationExterne,
                    roleAdministrator
                }.ForEach(b =&gt; context.Roles.AddOrUpdate(m =&gt; m.RoleName, b));
</code></pre>

<p>Then I can do:</p>

<pre><code>var myUser = new User
      {
        Company = "DELTOY",
        FirstName = "BRYAN",
        LastName = "KENETH",
        Roles = new List&lt;Role&gt; { roleEditionCNT }
      };
</code></pre>

<p>Now I would like to modify the way I assign roles like this:</p>

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

<p>This code compiled successfully but at runtime I got the error: </p>

<blockquote>
  <p>Violation of PRIMARY KEY constraint 'PK_Roles'. Cannot insert
  duplicate key in object 'dbo.Roles'</p>
</blockquote>

<p>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.</p>

<p>Any idea?</p>
