实体框架4 CTP 5自我参照多对多 [英] Entity Framework 4 CTP 5 Self Referencing Many-to-Many
问题描述
我的代码看起来像这样:
public class Study
{
public int ID {get;组; }
public string主题{get;组; }
public byte TypeID {get;组; }
public virtual StudyType Type {get;组; }
public bool不推荐使用{get;组; }
public virtual ICollection< Study>先决条件{get;组; }
}
public class StudyType
{
public byte ID {get;组; }
public string Name {get;组; }
public virtual ICollection< Study>研究{get;组; }
}
public class MyContext:DbContext
{
public DbSet< Study>研究{get;组; }
public DbSet< StudyType> StudyTypes {get;组;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity< Study>()
.HasMany(p => p.Prerequisites)
.WithMany()。Map(ps =>
{
ps.ToTable(先决条件);
ps.MapLeftKey(x => x.ID,StudyID );
ps.MapRightKey(y => y.ID,PrerequisiteID);
});
}
我的EF语法不是很好,发现谷歌,似乎应该工作。相反,我得到序列包含多个匹配的元素
。
我发现这个,但是由于实体引用本身,我只能在一个表中完全重命名密钥字段: http://social.msdn.microsoft.com/Forums/eu/adonetefx/thread/745a2c4f-cb66-41ad-9524-15aa198c40c7
有人帮助我吗?
编辑
以下是异常的完整堆栈跟踪:
它在一行LINQ上执行:$ code> var x = from db in the db.Studies select s;
服务器堆栈跟踪:
在System.Linq.Enumerable.SingleOrDefault [ TSource](IEnumerable`1源,Func`2谓词)
在System.Data.Entity.ModelConfiguration.Configuration.Properties.Navigation.ManyToManyAssociationMappingConfiguration`2.Configure(DbAssociationSetMa pping associationSetMapping)
在System.Data.Entity.ModelConfiguration.Configuration.Properties.Navigation.NavigationPropertyConfiguration.Configure(DbDatabaseMapping databaseMapping)
在System.Data.Entity.ModelConfiguration.Utilities.IEnumerableExtensions.Each [T] (IEnumerable`1 TS,Action`1动作)
在System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.ConfigureAssociationMappings(DbDatabaseMapping databaseMapping)
。在System.Data.Entity.ModelConfiguration.Configuration。 Types.EntityTypeConfiguration.Configure(DbEntityTypeMapping entityTypeMapping,DbDatabaseMapping databaseMapping,DbProviderManifest providerManifest)
在System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(DbDatabaseMapping databaseMapping,DbProviderManifest providerManifest)
在System.Data.Entity .ModelConfiguration.ModelBuilder.Build(DbProviderManifest providerManifest,DbProviderInfo providerInfo,Boolean validateModel)
在System.Data.Entity.ModelConfiguration.ModelBuilder.Build(的DbConnection providerConnection)在System.Data.Entity.Internal.LazyInternalContext.CreateModel
()在System.Lazy`1.CreateValue
()
异常在[0]重新抛出:
在System.Linq.Enumerable.SingleOrDefault [TSource](IEnumerable`1源,Func`2谓词)
在System.Data.Entity。 SystemConfiguration.Properties.Navigation.NavigationPropertyConfiguration.Configure(DbDatabaseMapping databaseMapping)
在System。 Data.Entity.ModelConfiguration.Utilities.IEnumerableExtensions.Each [T](IEnumerable`1 ts,Action`1动作)
在System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.ConfigureAssociationMappings(DbDatabaseMapping databaseMapping)
在System.Data.Entity.ModelConfigur ation.Configuration.Types.EntityTypeConfiguration.Configure(DbEntityTypeMapping entityTypeMapping,DbDatabaseMapping databaseMapping,DbProviderManifest providerManifest)
在System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(DbDatabaseMapping databaseMapping,DbProviderManifest providerManifest)
在系统.Data.Entity.ModelConfiguration.ModelBuilder.Build(DbProviderManifest providerManifest,DbProviderInfo providerInfo,Boolean validateModel)
在System.Data.Entity.ModelConfiguration.ModelBuilder.Build(DbConnection providerConnection)
在System.Data.Entity .Internal.LazyInternalContext.CreateModel()
在System.Lazy`1.CreateValue()在System.Lazy`1.LazyInitValue
()
在System.Data.Entity.Internal.LazyInternalContext .InitializeContext()
在System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(类型的EntityType)在System.Data.Entity.Internal.Linq.InternalSet`1.Initialize
()
在System.Dat a.Entity.Internal.Linq.InternalSet`1.get_Provider()
在System.Linq.Queryable.Select [TSource,TResult](IQueryable`1 source,Expression`1 selector)
在DataAccess。 Sql.SqlStudyRepository.GetAll()在C:\Side Work\Rephidim Church\Tuchikos 2011\Program\DataAccess\Sql\SqlStudyRepository.cs:在API.Controllers.StudiesController线22
。 C:\Side Work\Rephidim Church\Tuchikos中的索引()(\Side Work\Rephidim Church\Tuchikos 2011 \Program\API\Controllers\StudiesController.cs:第24行
在lambda_method(Closure,ControllerBase,Object [])
在System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary`2参数)
在System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext,ActionDescriptor actionDescriptor,IDictionary`2参数)
在System.Web.Mvc.ControllerActionInvoker。 c__DisplayClass15。< InvokeActionMethodWithFilters> b__12()
在Sys tem.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter滤波器,ActionExecutingContext preContext,Func`1续)
这是我在CTP5中类似情况下在EntityTypeConfiguration<>实现中所拥有的。
HasMany(g => g.SubGroups)
.WithMany(g => g.ParentGroups)
.Map(m => m.ToTable(Groups_SubGroups));
不知道如何翻译直接配置DbContext,但我想象应该是接近的。
如果内存服务,则在CTP5中,LeftKey()RightKey()语法不完整,因此您只需使用其创建或期望的默认列名称。在我的情况下,它是GroupId和GroupId1。这遵循模式< class> Id和< class> Id1,而不是< field>和< field> 1。
您所获得的错误似乎很熟悉,我不记得解决方案是以任何方式显而易见的。但是,我一直以来都设置了这一切,所以我如何到达某些工作的回忆是有点瑞士干酪。希望有帮助。
I have the following scenario in my database. It is a record of Studies and those studies have other studies as prerequisites. In my DB design, it looks like this:
And my code looks something like this:
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");
});
}
I'm not super good at the EF syntax, but from what I've found Googling, that seems like it should work. Instead, I get Sequence contains more than one matching element
.
I found this, but since the entity is referencing itself, I can't exactly rename the key field in only one of the tables: http://social.msdn.microsoft.com/Forums/eu/adonetefx/thread/745a2c4f-cb66-41ad-9524-15aa198c40c7
Anybody help me through this?
EDIT
Here is the full stack trace of the exception:
It executes on a line of 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)
This is what I have in an EntityTypeConfiguration<> implementation for a similar situation in CTP5.
HasMany(g => g.SubGroups)
.WithMany(g => g.ParentGroups)
.Map(m => m.ToTable("Groups_SubGroups"));
Not sure exactly how that translates to configuring the DbContext directly, but I imagine it should be close.
If memory serves, LeftKey() RightKey() syntax wasn't quite there in CTP5, so you just have to use the default column names that it creates or is expecting. In my case, it is GroupId and GroupId1. That follows the pattern <class>Id and <class>Id1, not <field> and <field>1 by the way.
The error that you're getting does seem familiar and I don't remember that the solution was obvious in any way. But, I did set this all up a while ago so the memories of how I arrived at something that works is a bit swiss cheesed. Hope it helps some.
这篇关于实体框架4 CTP 5自我参照多对多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!