Entity Framework many-to-many relationship

Search for a command to run...

No comments yet. Be the first to comment.
Phew, that was a lot. I'm always surprised how simple solutions contain so much detail and nuance when I try to explain them. I hope I conveyed the message that A-Frame simplifies code within a module or class by separating the infrastructure compone...

No architecture is complete without an easy way to test the functionality. My recommended strategy is to use two types of tests: unit and integration. As far as test setup goes, there is no clear winner for a test framework. xUnit.NET, NUnit and the ...

The examples in the previous posts seem really nice for simple scenarios. How do I approach more advanced use cases? The big scenarios are: I need multiple pieces of data from different sources I need to perform an infrastructure call in the middle...

I’ve shown how I write code using the A-Frame architecture without help. Yet I find a library or framework very convenient when using advanced techniques. Wolverine is at its core a messaging framework, but goes well beyond that. It has an in-memory ...

Now the expected structure is clear, let's take a look at the code. I will start with a minimal API implementation to demonstrate that there is no need for a framework to implement this architecture. I do know of a framework that makes A-Frame effort...

While optimising a query, I noticed that a many-to-many relationship still used a class in between. There is a more optimised way to configure many-to-many relationships in Entity Framework.
Before, many-to-many relationships looked like:
[Table("dbo.Book")]
public class Book
{
[Key]
public int Id { get; set; }
public virtual ICollection Authors { get; set; }
}
[Table("dbo.BookAuthor")]
public class BookAuthor
{
[Key]
public int BookId { get; set; }
[Key]
public int AuthorId { get; set; }
}
[Table("dbo.Author")]
public class Author
{
[Key]
public int Id { get; set; }
public virtual ICollection Authors { get; set; }
}
There is a much more compact way of describing it:
[Table("dbo.Person")]
public class Book
{
[Key]
public int Id { get; set; }
public virtual ICollection Authors { get; set; }
}
public class Author {
[Key]
public int Id { get; set; }
public virtual ICollection Books { get; set; }
}
public class MyContext : DbContext
{
public void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Author>()
.HasMany<Order>(a => a.Books)
.WithMany(b => b.Authors)
.Map(map =>
{
map.MapLeftKey("BookId");
map.MapRightKey("AuthorId");
map.ToTable("BookAuthor");
});
}
}
The OnModelCreating method will tell Entity Framework to add a many-to-many relationship between the Book and Author table. In the back it will use a link table named BookAuthor. Now, you won't see this little table anymore. One less step to take and to make your code a bit more readable. Just make sure the MapLeftKey and MapRightKey are set correctly, otherwise an existing table will map the wrong items to each other.
Btw, thanks Stack Overflow.