I've looked at a lot of questions that dealt with "IDENTITY_INSERT is ON" but I either can't seem to find one that helps me with my situation, or I'm new enough that I just don't understand how to fix it. So I apologize if this is a duplicate.
I am getting the following error when my code tries to run Roles.AddUserToRole(UserName, RoleName); I'm not sure what I did because I'm pretty sure I had this working at one time.
An explicit value for the identity column in table 'webpages_UsersInRoles' can only be specified when a column list is used and IDENTITY_INSERT is ON.
I've been using a combo of the following tutorials: http://tutlane.com/tutorial/aspnet-mvc/asp-net-mvc-membership-provider-to-create-users-roles-mapping-roles-to-users http://www.dotnetfunda.com/articles/show/2648/working-with-roles-in-aspnet-mvc-4
Account Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using DotNetOpenAuth.AspNet;
using Microsoft.Web.WebPages.OAuth;
using WebMatrix.WebData;
using AccountModule_Attempt_5.Filters;
using AccountModule_Attempt_5.Models;
namespace AccountModule_Attempt_5.Controllers
{
[Authorize]
[InitializeSimpleMembership]
public class AccountController : Controller
{
//ROLES///
[HttpGet]
public ActionResult DisplayAllRole()
{
IEnumerable<Role> ListRoles;
using (UsersContext db = new UsersContext())
{
ListRoles = db.Roles.ToList();
}
return View(ListRoles);
}
[NonAction]
public List<SelectListItem> GetAll_Users()
{
List<SelectListItem> listuser = new List<SelectListItem>();
listuser.Add(new SelectListItem { Text = "select", Value = "0" });
using (UsersContext db = new UsersContext())
{
foreach (var item in db.UserProfiles)
{
listuser.Add(new SelectListItem { Text = item.UserName, Value = item.UserName });
}
}
return listuser;
}
[HttpGet]
public ActionResult RoleAddToUser()
{
SelectList list = new SelectList(Roles.GetAllRoles());
ViewBag.Roles = list;
return View();
}
//Add User to Roll (adds multiple roles) http://www.dotnetfunda.com/articles/show/2648/working-with-roles-in-aspnet-mvc-4
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult RoleAddToUser(string RoleName, string UserName)
{
if (Roles.IsUserInRole(UserName, RoleName))
{
ViewBag.ResultMessage = "This user already has the role specified !";
}
else
{
**Roles.AddUserToRole(UserName, RoleName);** //This is the line that errors out.
ViewBag.ResultMessage = "Username added to the role succesfully !";
}
SelectList list = new SelectList(Roles.GetAllRoles());
ViewBag.Roles = list;
return View();
}
//Method for getting UserName by UserID : Here we Created GetUserName_BY_UserID Method for getting Users name from UserID and this Method is created inside Account Controller.
public string GetUserName_BY_UserID(int UserId)
{
using (UsersContext context = new UsersContext())
{
var UserName = (from UP in context.UserProfiles
where UP.UserId == UserId
select UP.UserName).SingleOrDefault();
return UserName;
}
}
View
@model AccountModule_Attempt_5.Models.AssignRoleVM
@{
ViewBag.Title = "RoleAddToUser";
}
<h2>Role Add to User</h2>
@using (Html.BeginForm("RoleAddToUser", "Account"))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="message-success">@ViewBag.ResultMessage</div>
<p>
Username : @Html.TextBox("UserName")
Role Name: @Html.DropDownList("RoleName", ViewBag.Roles as SelectList)
</p>
<input type="submit" value="Save" />
}
@*<h2>RoleAddToUser</h2>
<link href="~/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
<script src="~/bootstrap/js/bootstrap.min.js"></script>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
<div>
@ViewBag.ResultMessage
</div>
<fieldset>
<legend>AssignRoleVM</legend>
<div class="editor-label">
@Html.LabelFor(model => model.RoleName)
</div>
<div class="editor-field">
@Html.DropDownListFor(m => m.RoleName, new SelectList(Model.RolesList, "Value", "Text"),
new { style = "width:200px", @class = "form-control" })
@Html.ValidationMessageFor(model => model.RoleName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UserID)
</div>
<div class="editor-field">
@Html.DropDownListFor(m => m.UserID, new SelectList(Model.Userlist, "Value", "Text"),
new { style = "width:200px", @class = "form-control" })
@Html.ValidationMessageFor(model => model.UserID)
</div>
<br />
<p>
<input type="submit" value="Assign" />
</p>
</fieldset>
}*@
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
MODELS UserProfile
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace AccountModule_Attempt_5.Models
{
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
[Required]
public string UserName { get; set; }
public string EmailId { get; set; }
[Required]
public string CustomerId { get; set; }
}
}
AccountModel
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Globalization;
using System.Web.Security;
namespace AccountModule_Attempt_5.Models
{
public class UsersContext : DbContext
{
public UsersContext()
: base("DefaultConnection")
{
}
public DbSet<UserProfile> UserProfiles { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<webpages_UsersInRoles> webpages_UsersInRole { get; set; }
//public DbSet<RegisterModel> UserProfiles { get; set; }
//public DbSet<RegisterModel>Users { get; set; }
// public DbSet<UsersRole> UsersRoles { get; set; }
}
[Table("webpages_UsersInRoles")]
public class webpages_UsersInRoles
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public int RoleId { get; set; }
}
public class RegisterExternalLoginModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
public string ExternalLoginData { get; set; }
}
}
AssignRoleVM
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Web.Mvc;
namespace AccountModule_Attempt_5.Models
{
public class AssignRoleVM
{
[Required(ErrorMessage = " Select Role Name")]
public string RoleName { get; set; }
[Required(ErrorMessage = "Select UserName")]
public string UserID { get; set; }
public List<SelectListItem> Userlist { get; set; }
public List<SelectListItem> RolesList { get; set; }
}
}
Role
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace AccountModule_Attempt_5.Models
{
[Table("webpages_Roles")]
public class Role
{
[Required(ErrorMessage = "Enter Role name")]
//[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
public string RoleName { get; set; }
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int RoleId { get; set; }
}
}
I'm not sure what other code you need me to include, so tell me what would help and I'll include it. I've got the following Models: Account, AssignRoleVM, Customer, Role, UserProfile I am also using Entity Framework 6.0 and MVC 4.5