gpt4 book ai didi

entity-framework-4 - Entity Framework 4 CTP 5 自引用多对多

转载 作者:行者123 更新时间:2023-12-04 08:56:04 24 4
gpt4 key购买 nike

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

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)

最佳答案

这是我在 EntityTypeConfiguration<> 实现中针对 CTP5 中类似情况的内容。

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

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

如果没记错的话,LeftKey() RightKey() 语法在 CTP5 中并不完全存在,因此您只需要使用它创建或期望的默认列名。就我而言,它是 GroupId 和 GroupId1。顺便说一下,这遵循模式 Id 和 Id1,而不是 1。

您遇到的错误似乎很熟悉,我不记得解决方案在任何方面都很明显。但是,我不久前确实设置了这一切,所以我如何找到有效的东西的内存有点瑞士奶酪。希望能帮到一些人。

关于entity-framework-4 - Entity Framework 4 CTP 5 自引用多对多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4981228/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com