Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to get the priviliges associated with the roles by using linq... there is a many to many relationship between the database tables...
here is my code....

Table ROLE
C#
[Table("ROLES")]
   public partial class ROLE
   {
       public ROLE()
       {
           PRIVILIGES = new HashSet<PRIVILIGE>();
           USERS = new HashSet<USER>();
       }

       public int ID { get; set; }

       [Required]
       [StringLength(20)]
       public string RoleName { get; set; }

       [Required]
       [StringLength(50)]
       public string Description { get; set; }

       public virtual ICollection<PRIVILIGE> PRIVILIGES { get; set; }       
   }


Table PRIVILIGE

C#
[Table("PRIVILIGES")]
    public partial class PRIVILIGE
    {
        public PRIVILIGE()
        {
            ROLES = new HashSet<ROLE>();
        }

        public int ID { get; set; }

        [Required]
        [StringLength(50)]
        public string Description { get; set; }

        public virtual ICollection<ROLE> ROLES { get; set; }
    }


DbContextClass

C#
public partial class TestingContext : DbContext
    {
        public TestingContext()
            : base("name=TestingContext")
        {
        }

        public virtual DbSet<PRIVILIGE> PRIVILIGES { get; set; }
        public virtual DbSet<ROLE> ROLES { get; set; }
        public virtual DbSet<USER> USERS { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<PRIVILIGE>()
                .HasMany(e => e.ROLES)
                .WithMany(e => e.PRIVILIGES)
                .Map(m => m.ToTable("RolesPriviliges").MapLeftKey("Perviliges_PreviligeID").MapRightKey("Roles_RolesID"));

        }
    }
Posted
Comments
John C Rayan 26-Oct-15 10:48am    
what error are you getting?
Member 10740412 26-Oct-15 14:02pm    
public ICollection<roledto> GetPriviliges()
{
var query = from p in db.PRIVILIGES
from r in p.ROLES
select new
{
Description = p.Description
};
return query.ToList();
}

I am doing this to get the priviliges associated with the role but this error is coming....

Cannot implicitly convert type 'System.Collections.Generic.List<anonymoustype#1>' to 'System.Collections.Generic.ICollection<DTO.RoleDTO>'. An explicit conversion exists (are you missing a cast?)
John C Rayan 27-Oct-15 5:30am    
Can you try select new Role { } (or the correct type)
Member 10740412 27-Oct-15 15:21pm    
That error has been removed....
now when i call the GetPrivilige() function it gives me error....
Cannot implicitly convert type 'System.Collections.Generic.ICollection<DTO.RoleDTO>' to 'System.Collections.Generic.ICollection<DTO.PriviligeDTO>'. An explicit conversion exists (are you missing a cast?)
here is what i am trying....

public ICollection<roledto> GetPriviliges()
{
var query = (
from p in db.PRIVILIGES
from r in p.ROLES
select new
{
Priviliges = p.Description
}
).ToList() as ICollection<roledto>;

return query.ToList();
}
here is code where i am calling GetPrivilige function....
public List<roledto> GetData()
{
var query = from r in db.ROLES
select new RoleDTO
{
ID = r.ID,
RoleName = r.RoleName,
Description = r.Description,
Priviliges = GetPriviliges()
return query.ToList();
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900