In .NET Entity Framework, what is the best way to have a (custom) join table with extra attributes (other than ids) and/or associate this join table with others via separate
Yes, you can get something pretty close. I'm not quite sure how to set this up in the designer since I only work with codefirst.
Here's an example:
Student -> StudentFloor <- Floor
public class Student
{
public int Id { get; set; }
// ... properties ...
// Navigation property to your link table
public virtual ICollection StudentFloors { get; set; }
// If you wanted to have a property direct to the floors, just add this:
public IEnumerable Floors
{
get
{
return StudentFloors.Select(ft => ft.Floor);
}
}
}
The linking table:
public class StudentFloor
{
#region Composite Keys
// Be sure to set the column order and key attributes.
// Convention will link them to the navigation properties
// below. The database table will be created with a
// compound key.
[Key, Column(Order = 0)]
public int StudentId { get; set; }
[Key, Column(Order = 1)]
public int FloorId { get; set; }
#endregion
// Here's the custom data stored in the link table
[Required, StringLength(30)]
public string Room { get; set; }
[Required]
public DateTime Checkin { get; set; }
// Navigation properties to the outer tables
[Required]
public virtual Student Student { get; set; }
[Required]
public virtual Floor Floor { get; set; }
}
Finally, the other side of the many-to-many:
public class Floor
{
public int Id { get; set; }
// ... Other properties.
public virtual ICollection StudentFloors { get; set; }
}