0

I'm trying to learn how to use Sql Role Provider in a test mvc application. I have configured the web.config file and am connecting to a pre-existing sql server database (meaning I am not the one that created it).

        protected void Application_Start()
        {    
            if (Roles.RoleExists("Admin") == false)
            {
                Roles.CreateRole("Admin");
                Roles.AddUserToRole("DOMAIN\\username", "Admin");
            }
        }

This code fails on Roles.CreateRole with Cannot insert the value NULL into column 'RoleId'. From what I have read, this value should be auto-generated. Is this the case? Do I need to change RoleId to Is Identity, Yes?

user3738893
  • 425
  • 1
  • 6
  • 18

2 Answers2

0

for learn about identity in mvc5 try with this example is very clear

create a empty c# web app, and download

PM> Install-Package Microsoft.AspNet.Identity.Samples -Pre

The db is code first, it's create automatic, change the connection string in webconfig for change where is create the db.

This sample contains all functionalities about users and roles for more info:

the sample for mvc5 in vs2013 not contains all functions to use roles. is needed make more work to started

http://typecastexception.com/post/2014/06/22/ASPNET-Identity-20-Customizing-Users-and-Roles.aspx

chefjuanpi
  • 1,570
  • 15
  • 27
  • 1
    Sure. Your question appeared in the moderation queue just to show the S.O. practices to the new users. – borjab Nov 03 '14 at 17:36
0

Enable the rolemanger in webconfig file. Modify or create the below code in your project webconfig file and then try the same

<system.web>
 <roleManager enabled="true" />
</system.web>
Aravindan
  • 855
  • 6
  • 15