简体   繁体   English

多对多映射不起作用-EF 4.1 RC

[英]Many to Many mapping not working - EF 4.1 RC

UPDATE: After a bit more research it seems a number of my many-to-many mappings aren't working. 更新:经过了些研究,似乎我的一些许多一对多映射的不工作。 Hmmm... 嗯...

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. 我正在将数据访问项目从EF 4.1 CTP4升级到EF 4.1 RC,并且在使用新的EntityTypeConfiguration<T>设置时遇到了麻烦。

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. 当我尝试获取.First()项时,我得到的Sequence contains no elements异常。

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. 只是说没有项目, 我知道集合中应该有项目-因此我的新映射肯定有问题。

Here's the code I have so far: 这是我到目前为止的代码:

Product Model 产品型号

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 Mapping the worked! 以前的CTP4映射有效!

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

Can anyone see anything that needs fixing? 有人能看到需要修复的东西吗? Let me know if you want me to provide more code. 如果您要我提供更多代码,请告诉我。

EDIT: More Code 编辑:更多代码

DbTable 数据库表

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

Tag 标签

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

Yes, this is a legacy database with naming conventions up to boohai . 是的,这是一个旧数据库,其命名约定最多为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. 我知道Tag类必须正确连接,因为Product具有另一个属性Specialization ,它也映射到Tag并且可以正确加载。 But do note that it's mapped in a one-to-many manner. 但请注意,它是以一对多的方式映射的。 So it seems to be the many-to-many with Tag . 因此,似乎与Tag是多对多的。

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. 您需要同时指定两个导航属性才能进行多对多映射。

Try adding the lambda in the WithMany property pointing back to the products: 尝试在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");
            });

(crossing fingers...) (手指交叉...)

I haven't used the Code-First approach yet, but when working with POCOs I had to enable Lazy-Loading, to make Navigation Properties work. 我还没有使用“代码优先”方法,但是在使用POCO时,我必须启用“延迟加载”才能使“导航属性”正常工作。 This is of course by design, but I don't know if you have to explicitly enable this behavior for Code-First. 这当然是设计使然,但是我不知道您是否必须为Code-First明确启用此行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM