本文介绍了如何在实体框架中添加值外键refferenced列6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<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<Task>();
this.Users = new HashSet<User>();
}
public int RoleID { get; set; }
public string RoleName { get; set; }
public virtual ICollection<Task> Tasks { get; set; }
}</pre>
task.cs is like
<pre lang="cs">public partial class Task
{
public Task()
{
this.Users_Tasks = new HashSet<Users_Tasks>();
this.Roles = new HashSet<Role>();
}
public int TaskID { get; set; }
public string TaskName { get; set; }
public string TaskDescription { get; set; }
public virtual ICollection<Role> Roles { get; set; }
}</pre>
I want add task to role ,
please help me,
thanks and regards
推荐答案
var role = db.Roles.Where(r => r.RoleID == roleId).First();
role.Tasks.Add(db.Tasks.First(c => c.TaskID == taskId));
db.SaveChanges();
这篇关于如何在实体框架中添加值外键refferenced列6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!