プロジェクトでこの問題が発生しました。ドメインスコープ全体を提供したくないため、クラスをできるだけ単純化しようとしました。
基本的に私は基本クラスを持っていBaseClass
ます.2つのプロパティが拡張されているクラスを参照していますBaseClass
=>ClassA
ClassB を削除すると動作します。
2 つの参照のいずれかを削除すると機能します。から
拡張しない場合に機能しますClassA
BaseClass
しかし、この星座ではありません:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Threading;
namespace EFTestbed
{
public abstract class BaseClass
{
public int Id { get; set; }
public ClassA ClassA1 { get; set; }
public ClassA ClassA2 { get; set; }
}
public class ClassA : BaseClass
{
public string TitleA { get; set; }
}
public class ClassB : BaseClass
{
public string TitleB { get; set; }
}
public class Context : DbContext
{
public DbSet<ClassA> ClassAs { get; set; }
public DbSet<ClassB> ClassBs { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ClassA>()
.HasOptional(e => e.ClassA1).WithOptionalDependent();
modelBuilder.Entity<ClassA>()
.HasOptional(e => e.ClassA2).WithOptionalDependent();
modelBuilder.Entity<ClassB>()
.HasOptional(e => e.ClassA1).WithOptionalDependent();
modelBuilder.Entity<ClassB>()
.HasOptional(e => e.ClassA2).WithOptionalDependent();
}
}
public class Program
{
static void Main(string[] args)
{
Thread.CurrentThread.CurrentCulture =
Thread.CurrentThread.CurrentUICulture =
new System.Globalization.CultureInfo("en-GB");
System.Console.WriteLine("Creating & Updating database...");
Database.SetInitializer(new DropCreateDatabaseAlways<Context>());
using (var context = new Context())
{
context.ClassAs.Add(new ClassA() { TitleA = "Test1" });
System.Console.WriteLine(
string.Format("Number of objects written to the database: {0}",
context.SaveChanges()));
}
System.Console.Write("Press Enter to continue...");
System.Console.ReadLine();
}
}
}
私は常にこの例外「シーケンスに複数の要素が含まれています」が発生します
どうすれば機能しますか?
例外のスタック トレースは次のとおりです。
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
at System.Data.Entity.ModelConfiguration.Configuration.Properties.Navigation.NavigationPropertyConfiguration.ConfigureDependentBehavior(EdmAssociationType associationType, EdmModel model, EntityTypeConfiguration entityTypeConfiguration)
at System.Data.Entity.ModelConfiguration.Configuration.Properties.Navigation.NavigationPropertyConfiguration.Configure(EdmNavigationProperty navigationProperty, EdmModel model, EntityTypeConfiguration entityTypeConfiguration)
at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.ConfigureAssociations(EdmEntityType entityType, EdmModel model)
at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.Configure(EdmEntityType entityType, EdmModel model)
at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntities(EdmModel model)
at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(EdmModel model)
at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.Initialize()
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_InternalContext()
at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)
at System.Data.Entity.DbSet`1.Add(TEntity entity)
at EFTestbed.Program.Main(String[] args) in d:\users\sasp1de\documents\visual studio 2010\Projects\EFTestbed\EFTestbed\TestEF\Program.cs:line 54
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()