Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
XML
<b></b>hi all,

I am working on a class file that uses entity framework 6,

   I have two object files
   roles.cs and task.cs

   role.cs is like
<pre lang="cs">public partial class Role
    {
        public Role()
        {
            this.Tasks = new HashSet&lt;Task&gt;();
            this.Users = new HashSet&lt;User&gt;();
        }

        public int RoleID { get; set; }
        public string RoleName { get; set; }

        public virtual ICollection&lt;Task&gt; Tasks { get; set; }

    }</pre>
 task.cs is like

<pre lang="cs">public partial class Task
   {
       public Task()
       {
           this.Users_Tasks = new HashSet&lt;Users_Tasks&gt;();
           this.Roles = new HashSet&lt;Role&gt;();
       }

       public int TaskID { get; set; }
       public string TaskName { get; set; }
       public string TaskDescription { get; set; }


       public virtual ICollection&lt;Role&gt; Roles { get; set; }
   }</pre>


   I want add task to role ,
   please help me,
   thanks and regards
Posted

1 solution

hi all,

Got solution


we can add it like this

C#
var role = db.Roles.Where(r => r.RoleID == roleId).First();
 role.Tasks.Add(db.Tasks.First(c => c.TaskID == taskId));
 db.SaveChanges();
 
Share this answer
 

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