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