Entity Framework CTP5 (Code First) Modeling - lookup tables
Asked Answered
L

1

6

Assume the following table structure:

Tables:

**Tasks**
taskID int PK
taskName varchar

**Resources**
resourceID int PK
resourceName varchar

**Assignments**
assignmentID int PK
taskID int FK
resourceID int FK

The assignments table relates a task with the resource that is assigned to it. Is it possible to map this structure with the model builder so that I do not have to create an Assignment poco class - hiding some of the underlying data structure?

I.E.:

public class Task
{
    public int taskID { get; set; }
    public string taskName { get; set; }

    public virtual ICollection<Resource> resourceItems { get; set; }
}

public class Resource
{
    public int resourceID { get; set; }
    public string resourceName { get; set; }
}

How can I use the model builder to map tasks to resources without creating an assignment poco class?

Lawanda answered 22/2, 2011 at 16:17 Comment(0)
N
3

Here is an article about this very thing.

Edit, I dont have an IDE in front of me so this might not be the exact "latest" syntax, but it should get you started:

modelBuilder.Entity<Task>().HasMany(a => a.Resources).WithMany(b => b.Tasks).Map(m => 
{ 

  m.MapLeftKey(a => a.TaskId,"taskId");

  m.MapRightKey(b => b.ResourceId, "resourceId");

  m.ToTable("Assignments"); 

});
Newsman answered 22/2, 2011 at 16:32 Comment(1)
In that article EF is doing that in the ssdl & csdl. I need to know how to map it in code using the model builder. I may have no been clear in my question that I'm using the code-first approach in CTP5. I've updated my title.Lawanda

© 2022 - 2024 — McMap. All rights reserved.