Chris Klepeis
Chris Klepeis

Reputation: 9973

Entity Framework CTP5 (Code First) Modeling - lookup tables

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?

Upvotes: 6

Views: 1337

Answers (1)

mxmissile
mxmissile

Reputation: 11673

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"); 

});

Upvotes: 3

Related Questions