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
partialofIdentityRole. I would make another class derived fromIdentityRole.– Leonel Sanches da Silva
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.
– Leonel Sanches da Silva
I said crap. Then I’ll give you an answer ;)
– Leonel Sanches da Silva
That’s it. Sorry about the outside ball. I’m a little tired today.
– Leonel Sanches da Silva