更新:經過多一點研究後,似乎我的多對多映射的數字不起作用。嗯...多對多映射無法正常工作 - 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
多對多。
我將開始檢查是否有任何多對多關聯正在工作。
問題必須在其他地方,因爲我只是用你的代碼,它的工作原理爲了我。 – 2011-03-24 22:24:55
Niggly!儘管我非常感謝你試用我的代碼 - 這不是我想聽到的!我開啓了MARS,因爲我認爲這可能是一個問題......我還能從哪裏開始尋找? – Charlino 2011-03-24 22:47:31
很難說。您必須顯示其他代碼片段,因爲問題可能不在您顯示的那些中。 – 2011-03-25 08:14:29