How to extend ASP IdentityRole in EF DbFirst
Asked Answered
S

0

2

How can I extend the IdentityRoles to work with an extended AspNetRoles Table? in ASP MVC 5, with Identity 2.2 I looked here & here the answers do not help with DB First EF

Step 1: I extended/added the CompanyId & DescRole (to make Role unique by company/tenant)

CREATE TABLE [dbo].[AspNetRoles]            // EXTENDED role table
    [Id] [nvarchar](50) NOT NULL,
    [Name] [nvarchar](50) NOT NULL,
    [Description] [nvarchar](50) NOT NULL,  // EXTENDED
    [CompanyId] [nvarchar](50) NOT NULL,    // EXTENDED Company/Tenant

Step 2: Create new Role is an issue, now my create role fails, and I cannot find the extended CompanyId/Desc properties on the IdentityRole (its a dbFirst EF strategy)


 //Create ROle Class
 public ActionResult CreateRole(FormCollection collection)
 {
   try {

    var context = new ApplicationDbContext();
    // RoleStore
    // var newRole = new AspNetRoles();
    // ontext.Roles.Add(new ApplicationRole) { // not visible...
    context.Roles.Add(new Microsoft.AspNet.Identity.EntityFramework.IdentityRole()
       {
            Name = collection["RoleName"],                    
       });
    context.SaveChanges();
    ViewBag.Message = "Role created successfully !";
    ...

 //Extended Role Class
 public class ApplicationRole : IdentityRole
 {
   public ApplicationRole() : base() { }
   public ApplicationRole(string name) : base(name) { }
   public string Description { get; set; }
   public string CompanyId { get; set; }
 }
Surbase answered 7/4, 2017 at 4:48 Comment(2)
drive.google.com/open?id=0B5ryXIU3QbldTC05T3p6Q3JqNDgEleph
this project implements extending roles in ASP.NET Identity with Web API(Not in MVC). I believe you can achieve the same in mvc also.Eleph

© 2022 - 2024 — McMap. All rights reserved.