更新:经过多一点研究后,似乎我的多对多映射的数字不起作用。嗯...多对多映射无法正常工作 - 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