views:

214

answers:

0

I have the following:

  • Product POCO
  • ProductConfiguration class for EF code only (CTP3)
  • Product table in database
  • ProductCrossSell table in database

My Product and ProductConfiguration class looks like:

public class Product
{
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
    public virtual ICollection<Product> CrossSells  { get; set; }
}

public class ProductConfiguration : EntityConfiguration<Product>
{
    public ProductConfiguration()
    {
        HasKey(p => p.Id);
        Property(p => p.Name).IsRequired().IsUnicode().IsVariableLength().HasMaxLength(254);

        // Relationship??

        MapSingleType(p => new
                                {
                                    p.Id,
                                    p.Name
                                }).ToTable("Product");
    }
}

My database tables look like

CREATE TABLE [dbo].[Product](
[Id] [uniqueidentifier] NOT NULL,
[Name] [nvarchar](254) NOT NULL

CREATE TABLE [dbo].[ProductCrossSell](
[Id] [uniqueidentifier] NOT NULL,
[ProductId] [uniqueidentifier] NOT NULL

This is simply the Product table having a many-to-many relation to itself where products can be linked to other products.

Does anyone know how to set this relationship up in EF code only? I see examples of other relationships being setup in code but no examples of how to set this up.

When successful GetProductById query would execute, EF would join the two tables and populate the Product object as well as the CrossSells IColletion.

Any help is appreciated.

Thanks