实体框架4 CTP 5自我引用多对多

时间:2011-02-12 23:11:05

标签: entity-framework-4 many-to-many entity-framework-ctp5 self-reference

我的数据库中有以下场景。这是一项研究记录,这些研究将其他研究作为先决条件。在我的数据库设计中,它看起来像这样:

self referencing many to many

我的代码看起来像这样:

public class Study
{
    public int ID { get; set; }
    public string Topic { get; set; }
    public byte TypeID { get; set; }
    public virtual StudyType Type { get; set; }
    public bool Deprecated { get; set; }

    public virtual ICollection<Study> Prerequisites { get; set; }
}

public class StudyType
{
    public byte ID { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Study> Studies { get; set; }
}

public class MyContext : DbContext
{

    public DbSet<Study> Studies { get; set; }
    public DbSet<StudyType> StudyTypes { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Study>()
            .HasMany(p=>p.Prerequisites)
            .WithMany().Map(ps =>
                {
                    ps.ToTable("Prerequisites");
                    ps.MapLeftKey(x=>x.ID,"StudyID");
                    ps.MapRightKey(y=>y.ID,"PrerequisiteID");
                });
    }

我不是非常擅长EF语法,但是从我发现谷歌搜索,看起来它应该工作。相反,我得到Sequence contains more than one matching element

我发现了这一点,但由于实体正在引用自身,我无法仅在其中一个表中重命名关键字段:http://social.msdn.microsoft.com/Forums/eu/adonetefx/thread/745a2c4f-cb66-41ad-9524-15aa198c40c7

有人帮我解决了这个问题吗?

修改

以下是异常的完整堆栈跟踪:

它在LINQ:var x = from s in db.Studies select s;

的一行上执行
Server stack trace: 
 at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
 at System.Data.Entity.ModelConfiguration.Configuration.Properties.Navigation.ManyToManyAssociationMappingConfiguration`2.Configure(DbAssociationSetMapping associationSetMapping)
 at System.Data.Entity.ModelConfiguration.Configuration.Properties.Navigation.NavigationPropertyConfiguration.Configure(DbDatabaseMapping databaseMapping)
 at System.Data.Entity.ModelConfiguration.Utilities.IEnumerableExtensions.Each[T](IEnumerable`1 ts, Action`1 action)
 at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.ConfigureAssociationMappings(DbDatabaseMapping databaseMapping)
 at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.Configure(DbEntityTypeMapping entityTypeMapping, DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
 at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
 at System.Data.Entity.ModelConfiguration.ModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo, Boolean validateModel)
 at System.Data.Entity.ModelConfiguration.ModelBuilder.Build(DbConnection providerConnection)
 at System.Data.Entity.Internal.LazyInternalContext.CreateModel()
 at System.Lazy`1.CreateValue()

Exception rethrown at [0]: 
 at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
 at System.Data.Entity.ModelConfiguration.Configuration.Properties.Navigation.ManyToManyAssociationMappingConfiguration`2.Configure(DbAssociationSetMapping associationSetMapping)
 at System.Data.Entity.ModelConfiguration.Configuration.Properties.Navigation.NavigationPropertyConfiguration.Configure(DbDatabaseMapping databaseMapping)
 at System.Data.Entity.ModelConfiguration.Utilities.IEnumerableExtensions.Each[T](IEnumerable`1 ts, Action`1 action)
 at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.ConfigureAssociationMappings(DbDatabaseMapping databaseMapping)
 at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.Configure(DbEntityTypeMapping entityTypeMapping, DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
 at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
 at System.Data.Entity.ModelConfiguration.ModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo, Boolean validateModel)
 at System.Data.Entity.ModelConfiguration.ModelBuilder.Build(DbConnection providerConnection)
 at System.Data.Entity.Internal.LazyInternalContext.CreateModel()
 at System.Lazy`1.CreateValue()
 at System.Lazy`1.LazyInitValue()
 at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
 at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
 at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
 at System.Data.Entity.Internal.Linq.InternalSet`1.get_Provider()
 at System.Linq.Queryable.Select[TSource,TResult](IQueryable`1 source, Expression`1 selector)
 at DataAccess.Sql.SqlStudyRepository.GetAll() in C:\Side Work\Rephidim Church\Tuchikos 2011\Program\DataAccess\Sql\SqlStudyRepository.cs:line 22
 at API.Controllers.StudiesController.Index() in C:\Side Work\Rephidim Church\Tuchikos 2011\Program\API\Controllers\StudiesController.cs:line 24
 at lambda_method(Closure , ControllerBase , Object[] )
 at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
 at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
 at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
 at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)

1 个答案:

答案 0 :(得分:0)

这就是我在EntityTypeConfiguration中所拥有的&lt;&gt;针对CTP5中类似情况的实施。

HasMany(g => g.SubGroups)
    .WithMany(g => g.ParentGroups)
    .Map(m => m.ToTable("Groups_SubGroups"));

不确定如何转换为直接配置DbContext,但我想它应该接近。

如果内存服务,则在CTP5中,LeftKey()RightKey()语法不完全存在,因此您只需使用它创建或期望的默认列名。就我而言,它是GroupId和GroupId1。这遵循模式&lt; class&gt; Id和&lt; class&gt; Id1,而不是&lt; field&gt;和顺便说一句&lt; field&gt;。

你得到的错误看起来很熟悉,我不记得解决方案在任何方面都是显而易见的。但是,我确实在不久之前完成了这一切,所以关于我如何到达有效的东西的记忆有点瑞士奶酪。希望它有所帮助。