[英]Multiple foreign keys to many tables
I have the following classes: 我有以下课程:
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 ??
}
Basically: 基本上:
How will I write the database, as well as write the EF code to achieve this? 如何编写数据库以及编写EF代码以实现此目的? I want to be able to use Linq syntax for eg.
我希望能够将Linq语法用于例如。
List<Message> messages = project.Messages.ToList();
I have found this alternative design but not sure how the navigation properties will look. 我找到了这种替代设计,但不确定导航属性的外观。
I think you are pretty much there. 我想你几乎在那里。 Could you try this?
你可以试试这个吗?
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;}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.