It seems to me you wanted to configure a many-to-many relationship between Distributors and Manufacturers: Every Distributor has zero or more Manufacturers, every Manufacturer delivers to zero or more Distributors.
If you'd configured this many-to-many relationship according to the entity framework code first many-to-many conventions, you would have something like:
class Distributor
{
public int Id {get; set;}
public string Name {get; set;}
// a distributor has exactly one Address using foreign key:
public int AddressId {get; set;}
public Address Address {get; set;}
// a Distributor has zero or more Manufacturers: (many-to-many)
public virtual ICollection<Manufacturer> Manufacturers {get; set;}
// a Distirbutor has zero or more Users: (one-to-many)
public virtual ICollection<User> Users {get; set;}
}
class Manufacturer
{
public int Id {get; set;}
public string Name {get; set;}
// a Manufacturer has exactly one Address using foreign key:
public int AddressId {get; set;}
public Address Address {get; set;}
// a Manufacturer has zero or more Distributors (many-to-many)
public virtual ICollection<Distributor> Distributors {get; set;}
}
There is also a User: every User belongs to exactly one Distributor
class User
{
public int Id {get; set;}
// a user belongs to exactly one Distributor, using foreign key:
public int DistributorId {get; set;}
public virtual Distributor Distributor {get; set;}
...
}
Finally the DbContext
class MyDbContext : DbContext
{
public DbSet<Distributor> Distributors {get; set;}
public DbSet<Manufacturer> Manufacturers {get; set;}
public DbSet<User> Users {get; set;}
public DbSet<Address> Addresses {get; set;}
}
The above is all that entity framework needs to know to understand that you want a many-to-many between Distributors and ManuFacturers. Entity Framework will created a proper junction table for you, although you won't need it in your queries as I'll show you below. If you don't like the default junction table that entity framework creates for you, you can use fluent API to define the table and column names:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Distributor>()
.HasMany(distributor=> distributor.Manufacturers)
.WithMany(manufacturer => manufacturer.Distributors)
.Map(map =>
{
map.MapLeftKey("DistributorId");
map.MapRightKey("ManufacturerId");
map.ToTable("DistributorsManufacturers");
});
Although internally entity framework will use the junction table, you won't use it in your queries, just use the ICollections:
I've got a _user and I want several properties of the zero or one
Distributor of this user, together with several properties of all the
Manufacturers of this Distributor
Although the Include statement can be used, it is seldom wise to do so. One of the slower parts of database queries is the transfer of the selected data to your process, so you should limit the amount of transferred data to only the properties you really plan to use. Include will transfer all properties, and I highly doubt whether you'll use them, especially all the foreign keys with all the same values.
So your query using the ICollection:
var _user = ... // I've got a User
var result = dbContext.Distributers
.Where(distributor => distributor.Id == _user.DistributorId)
.Select(distributor => new
{
// select only the distributor properties you plan to use
Id = distributor.Id,
Name = distributor.Name,
Address = new
{
// again: only the properties you plan to use
Street = distributor.Address.Street,
City = distributor.Address.City,
Zip = distributor.Address.Zip,
}),
// fetch (all or some) manufacturers of this distributor
Manufacturers = distributor.Manufacturers
.Where(manufacturer => manufacturer.Address.NavigationState == "CA")
.Select(manufacturer => new
{
// select only the properties you plan to use
// probably not the foreign key to the junction table
Name = manufacturer .Name,
Address = new {...},
...
})
.ToList(),
})
.SingleOrDefault();
It might be that you want some different properties, but you get the gist
d.DistributorManufacturers.DistributorId
condition it is applying it to the collection, not to each of the collection items. I recall seeing an example of how to achieve what you are trying but was so appalled by it that I moved on and no longer recall the details. I'll see if I can find it again for you...DistributorManufacturers
andManufacturers
class code?