开发者

EF4 code only approach for many-to-many relationships

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜