1

更新:经过更多研究,我的许多多对多映射似乎不起作用。嗯……

我正在将数据访问项目从 EF 4.1 CTP4 升级到 EF 4.1 RC,但我在使用新EntityTypeConfiguration<T>设置时遇到了问题。

具体来说,我遇到了多对多关系的问题。Sequence contains no elements当我尝试获取该.First()项目时,我遇到了异常。

特别的例外并不是那么有趣。它只是说没有项目,我知道集合中应该有项目 - 所以我的新映射肯定有问题。

这是我到目前为止的代码:

产品型号

public class Product : DbTable
{
    //Blah

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

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

基本配置

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);
    }
}

产品配置

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");
            });
    }
}

以前的 CTP4 映射工作!

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

任何人都可以看到任何需要修复的东西吗?如果您希望我提供更多代码,请告诉我。

编辑:更多代码

数据库表

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; }
}

标签配置

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

我知道Tag该类必须正确连接,因为 Product 具有另一个属性Specialization,该属性也映射到Tag并且正确加载。但请注意,它是以一对多的方式映射的。所以它似乎是多对多的Tag

我将开始检查是否有任何多对多关联有效。

4

2 回答 2

1

您需要指定两个导航属性才能进行多对多映射。

尝试在 WithMany 属性中添加指向产品的 lambda:

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");
            });

(交叉手指……)

于 2011-03-25T22:29:32.050 回答
0

我还没有使用 Code-First 方法,但是在使用 POCO 时,我必须启用延迟加载,才能使导航属性正常工作。这当然是设计使然,但我不知道您是否必须为 Code-First 显式启用此行为。

于 2011-03-24T21:41:04.063 回答