How to create an Nxn relationship in the Identityrole class with a class created by me?

Asked

Viewed 187 times

2

I have an automatic mapping of Controllers and its Actions running on application startup (file Global.asax).

I use this to give permission to the user Controller x Action if he owns that combination of access rights. Controller + Action + Role.

So I need to create a relationship MenuItem x Roles. But I don’t know how to add this to the app. It will be a relationship N x N.

So I have this:

public class MenuItem
{
    public MenuItem {  
        Roles = new HashSet<IdentityRole>();
    }

    [Key]
    public int Id { get; set; }

    [StringLength(40)]
    [Required(AllowEmptyStrings = false]
    public string Name { get; set; }

    [Required]        
    [ForeignKey("Menu")]
    public int MenuId { get; set; }
    public virtual Menu Menu { get; set; }

    [InverseProperty("MenuItems")]
    public virtual ICollection<IdentityRole> Roles { get; set; }
}

Then I tried:

namespace Microsoft.AspNet.Identity.EntityFramework
{
    public partial class IdentityRole
    {
        public IdentityRole {
            MenuItems = new HashSet<MenuItem>();
        }

        [InverseProperty("Roles")]
        public virtual ICollection<MenuItem> MenuItems { get; set; }
    }
}

However, Visual Studio complains error in some parts of the code saying that the Identityrole class exists both in Microsoft.AspNet.Identity.EntityFramewrk when in App.Domain (where are the classes of my application).


I tried too

Exclude the partial class of IdentityRole and left the class MenuItem with the declared navigation property thus:

public class MenuItem
{
    ...

    public virtual ICollection<IdentityRole> Roles { get; set; }
}

However, by doing so the EF creates a field MenuItem_Id within the table of Roles (AspNetRoles), understanding as a relationship 1 x N.

How to create this relationship?

  • I can’t remember if it’s possible to make a partial of IdentityRole. I would make another class derived from IdentityRole.

  • I find it very complicated to reuse what is written. Write other classes and use them. I think you can do with the Fluent API but the application will do a lot in automatic, and that makes the life of the programmer a hell.

  • I said crap. Then I’ll give you an answer ;)

  • That’s it. Sorry about the outside ball. I’m a little tired today.

1 answer

2

With the indication of the Gypsy became easy to solve.

I created another inherited class of IdentityRole, in it I added the relationship I need and started using this class instead of the IdentityRole.

Thus:

public class Role : IdentityRole
{
    public Role() {
        MenuItems = new HashSet<MenuItem>();
    }

    [InverseProperty("Roles")]
    public virtual ICollection<MenuItem> MenuItems { get; set; }
}

And I started using it:

public class MenuItem
{
    public MenuItem() {
        Roles = new HashSet<MenuItem>();
    }

    [InverseProperty("MenuItems")]
    public virtual ICollection<Role> Roles { get; set; }
}

And then a table for the relationship NxN was created and the module is working.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.