Note that there are some explanatory texts on larger screens.

plurals
  1. POError using AddUserToRole of SimpleMembership: INSERT statement conflicted with the FOREIGN KEY constraint
    primarykey
    data
    text
    <p>I'm using asp.net mvc4, c# and Entity Framework 5. I am trying to create a user and assign role to the user at the same time using simple membership (database first approach), but getting the following error at <code>Roles.AddUserToRole("nuser", "Admin");</code></p> <blockquote> <p>The INSERT statement conflicted with the FOREIGN KEY constraint "FK_webpages_UsersInRoles_webpages_Roles". The conflict occurred in database "DbClick2Eat", table "dbo.webpages_Roles", column 'RoleId'. The statement has been terminated.</p> </blockquote> <p>Here is my code:-</p> <pre><code>[HttpPost] [ValidateAntiForgeryToken] public ActionResult CreateUserByAdmin(RegisterModel register, string role) { try { if (Roles.RoleExists(role)) { var model = register ?? new RegisterModel(); WebSecurity.CreateUserAndAccount(model.UserName, model.Password); if (WebSecurity.UserExists(model.UserName)) Roles.AddUserToRole("admin1", "Admin"); } else ModelState.AddModelError("NoRole", "Please select a role."); var roles = Roles.GetAllRoles().ToList(); ViewBag.DeliveryArea = new SelectList(roles.Select(x =&gt; new { Value = x, Text = x }), "Value", "Text"); return View(); } catch (MembershipCreateUserException e) { ModelState.AddModelError("", AccountController.ErrorCodeToString(e.StatusCode)); } return View(); } </code></pre> <p>DB Schema</p> <pre><code>CREATE TABLE [dbo].[webpages_Roles]( [RoleId] [int] IDENTITY(1,1) NOT NULL, [RoleName] [nvarchar](256) NOT NULL, CONSTRAINT [PK_webpages_Roles] PRIMARY KEY CLUSTERED ( [RoleId] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] CREATE TABLE [dbo].[webpages_UsersInRoles]( [RoleId] [int] NOT NULL, [UserId] [int] NOT NULL, CONSTRAINT [PK_webpages_UsersInRoles] PRIMARY KEY NONCLUSTERED ( [RoleId] ASC, [UserId] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] ALTER TABLE [dbo].[webpages_UsersInRoles] WITH CHECK ADD CONSTRAINT [FK_webpages_UsersInRoles_UserProfile] FOREIGN KEY([UserId]) REFERENCES [dbo].[UserProfile] ([UserId]) GO ALTER TABLE [dbo].[webpages_UsersInRoles] CHECK CONSTRAINT [FK_webpages_UsersInRoles_UserProfile] GO ALTER TABLE [dbo].[webpages_UsersInRoles] WITH CHECK ADD CONSTRAINT [FK_webpages_UsersInRoles_webpages_Roles] FOREIGN KEY([RoleId]) REFERENCES [dbo].[webpages_Roles] ([RoleId]) GO ALTER TABLE [dbo].[webpages_UsersInRoles] CHECK CONSTRAINT [FK_webpages_UsersInRoles_webpages_Roles] </code></pre> <p>-> "Admin" is a role name</p> <blockquote> <p>RoleId =1, RoleName = Admin</p> </blockquote> <p>"Admin" Role exists in table.I can't fix it... Need Help</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload