我有以下课程:
public class Project
{
public int ID { get; set; }
public string Name { get; set; }
...
// Navigation
public virtual IEnumerable<Task> Tasks { get; set; }
//public virtual IEnumerable<Message> Messages { get; set; } // ???
}
public class Task
{
public int ID { get; set; }
public int ProjectID { get; set; }
public string Name { get; set; }
...
// Navigation
//public virtual IEnumerable<Message> Messages { get; set; } // ???
}
public class Message
{
public int ID { get; set; }
public string Comment { get; set; }
...
// Foreign Keys ??
}
基本上:
我将如何编写数据库,以及编写EF代码来实现这一目标?我希望能够使用Linq语法,例如。 List<Message> messages = project.Messages.ToList();
我找到了这个alternative design,但不确定导航属性的外观。
答案 0 :(得分:0)
我认为你几乎就在那里。你能试试吗?
public class Project
{
public int ID { get; set; }
public string Name { get; set; }
...
// Navigation
public virtual IEnumerable<Task> Tasks { get; set; }
public virtual IEnumerable<Message> Messages { get; set; }
}
public class Task
{
public int ID { get; set; }
public int ProjectID { get; set; }
public string Name { get; set; }
...
// Navigation
public virtual IEnumerable<Message> Messages { get; set; }
}
public class Message
{
public int ID { get; set; }
public string Comment { get; set; }
...
public int? ProjectId {get;set;}
public int? TaskId {get;set;}
public virtual Project {get;set;}
public virtual Task {get;set;}
}