1

更新:经过多一点研究后,似乎我的多对多映射的数字不起作用。嗯...多对多映射无法正常工作 - EF 4.1 RC

我正在升级数据访问项目从EF 4.1 CTP4到EF 4.1 RC,并且我在安装新的EntityTypeConfiguration<T>时遇到问题。

具体来说,我遇到了多对多关系的问题。当我试图获取.First()项目时,出现Sequence contains no elements例外。

这个特殊的例外并不是那么有趣。所有的意思是,没有任何项目但是我知道应该有集合中的项目 - 所以我的新映射必定存在问题。

下面的代码我到目前为止:

产品型号

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

上一个CTP4映射工作!

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

任何人都可以看到需要修复的东西吗?让我知道你是否希望我提供更多的代码。

编辑:更多的代码

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

是的,这是一个具有命名约定up to boohai的传统数据库。

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

我将开始检查是否有任何多对多关联正在工作。

+0

问题必须在其他地方,因为我只是用你的代码,它的工作原理为了我。 – 2011-03-24 22:24:55

+0

Niggly!尽管我非常感谢你试用我的代码 - 这不是我想听到的!我开启了MARS,因为我认为这可能是一个问题......我还能从哪里开始寻找? – Charlino 2011-03-24 22:47:31

+0

很难说。您必须显示其他代码片段,因为问题可能不在您显示的那些中。 – 2011-03-25 08:14:29

回答

1

你需要指定这两个导航属性来做多对多的映射。

尝试在WithMany属性指回产品添加拉姆达:

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

(交叉手指...)

+0

谢谢你,这帮了我 – Omu 2011-04-09 07:29:24

0

我还没有使用Code-First方法,但是在使用POCO时,我必须启用Lazy-Loading,才能使导航属性正常工作。这当然是通过设计,但我不知道是否必须为Code-First明确启用此行为。

+0

我该怎么做? #lazyweb – Charlino 2011-03-24 22:48:27

+0

好吧,我发现它默认启用。明确地将它设置为true也没有帮助:-( – Charlino 2011-03-24 22:55:35

+0

是的,用EF4.1的dbcontext,延迟加载是默认启用的。:) – 2011-03-25 23:02:09