Database Reference
In-Depth Information
Figure 6-3. A many-to-many relationship between workers and tasks
The WorkerTask link table contains nothing more than the foreign keys supporting the many-to-many
relationship.
To convert the association to an entity representing the WorkerTask link table, follow these steps.
1.
Create a WorkerTask POCO entity class, as shown in Listing 6-2.
2.
Replace the Tasks property of the Worker POCO entity with a WorkerTasks property of type
ICollection<WorkerTask> .
3.
Replace the Workers property of the Task POCO entity with a WorkerTasks property of type
ICollection<WorkerTask> .
4. Add an auto-property of type DbSet<WorkerTask> to your DbContext subclass.
The final model should look like the one shown in Listing 6-2.
Listing 6-2. The Final Data Model Including WorkerTask
[Table("Worker", Schema="Chapter6")]
public class Worker
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int WorkerId { get; set; }
public string Name { get; set; }
[ForeignKey("WorkerId")]
public virtual ICollection<WorkerTask> WorkerTasks { get; set; }
}
[Table("Task", Schema = "Chapter6")]
public class Task
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int TaskId { get; set; }
public string Title { get; set; }
[ForeignKey("TaskId")]
public virtual ICollection<WorkerTask> WorkerTasks { get; set; }
}
 
Search WWH ::




Custom Search