多对多映射不工作 - EF 4.1 RC工作、EF、RC

2023-09-07 14:12:29 作者:余 忆

更新:经过多一点的研究看来的若干的我的许多一对多的映射不工作。嗯...

UPDATE: After a bit more research it seems a number of my many-to-many mappings aren't working. Hmmm...

我升级的EF 4.1 CTP4数据访问项目,EF 4.1 RC和我无法与新的 EntityTypeConfiguration< T> 设置

I'm upgrading a data access project from EF 4.1 CTP4 to EF 4.1 RC and I'm having trouble with the new EntityTypeConfiguration<T> setup.

具体我有一个问题,一个多到多关系。我得到一个序列不包含任何元素例外,当我试图让。首先()项。

Specifically I'm having an issue with a Many-to-Many relationship. I'm getting a Sequence contains no elements exception when I'm trying to get the .First() item.

在特定的异常是不是真的那么有趣。所有它说的是,有没有项目的可是我知道应该有集合中的项目 - 所以必须有一个问题,我的新映射

The particular exception isn't really that interesting. All it's saying is that there are no items BUT I know there should be items in the collection - so there must be an issue with my new mappings.

这里的code我到目前为止有:

Here's the code I have so far:

产品型号

public class Product : DbTable
{
    //Blah

    public virtual ICollection<Tag> Categories { get; set; }

    public Product()
    {
        //Blah
        Categories = new List<Tag>();
    }
}

BaseConfiguration

public class BaseConfiguration<T> : EntityTypeConfiguration<T> where T : DbTable
{
    public BaseConfiguration()
    {
        this.HasKey(x => x.Id);
        this.Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        this.Property(x => x.UpdatedOn);
        this.Property(x => x.CreatedOn);
    }
}

ProductConfiguration

public class ProductConfiguration : BaseConfiguration<Product> 
{
    public ProductConfiguration()
    {
        this.ToTable("Product");

        //Blah

        this.HasMany(x => x.Categories)
            .WithMany()
            .Map(m =>
            {
                m.MapLeftKey("Tag_Id");
                m.MapRightKey("Product_Id");
                m.ToTable("ProductCategory");
            });
    }
}

previous CTP4映射的工作!

this.HasMany(x => x.Categories)
    .WithMany()
    .Map("ProductCategory", (p, c) => new { Product_Id = p.Id, Tag_Id = c.Id  });

任何人都可以看到任何需要修改的?让我知道如果你要我提供更多的code。

Can anyone see anything that needs fixing? Let me know if you want me to provide more code.

编辑:更多code

DBTABLE

public class DbTable : IDbTable
{
    public int Id { get; set; }
    public DateTime UpdatedOn { get; set; }
    public DateTime CreatedOn { get; set; }
}

标签

public class Tag
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Slug { get; set; }
    public bool Visible { get; set; }
    public virtual TagType TagType { get; set; }
}

TagConfiguration

public class TagConfiguration : EntityTypeConfiguration<Tag>
{
    public TagConfiguration()
    {
        this.ToTable("Tags");

        this.HasKey(x => x.Id);
        this.Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).HasColumnName("tag_id");
        this.Property(x => x.Name).HasMaxLength(300).HasColumnName("tag_name");
        this.Property(x => x.Slug).HasMaxLength(500).HasColumnName("tag_slug");
        this.Property(x => x.Visible).HasColumnName("tag_visible");
        this.HasRequired(x => x.TagType).WithMany(tt => tt.Tags).Map(m => m.MapKey("tagtype_id"));
    }
}

是的,这是与命名​​约定高达boohai遗留数据库。

我知道标签类必须正确连线,因为产品具有另一个属性专业化也被映射到标签并加载运行。但请注意,它被映射在一对多的方式。因此,它似乎是多到多用标签

I know the Tag class must be wired up correctly because Product has another property Specialization which is also mapped to Tag and it loads correctly. But do note that it's mapped in a one-to-many manner. So it seems to be the many-to-many with Tag.

我就开始检查,是否有很多-to-many关联工作。

I'll start checking out if any many-to-many associations are working.

推荐答案

您需要指定的两个的导航性能做多对多的映射。

You need to specify both navigation properties to do many to many mapping.

试着增加拉姆达在WithMany属性指回产品:

Try adding the lambda in the WithMany property pointing back to the products:

this.HasMany(x => x.Categories)
            .WithMany(category=>category.Products)
            .Map(m =>
            {
                m.MapLeftKey(t => t.TagId, "Tag_Id");
                m.MapRightKey(t => t.ProductId, "Product_Id");
                m.ToTable("ProductCategory");
            });

(交叉手指...)

(crossing fingers...)