异常,在基类中使用Entity Framework 5进行两次引用

时间:2012-09-27 17:24:55

标签: entity-framework entity-framework-5

我在一个项目中遇到了这个问题,因为我不想给出整个域范围,我试图尽可能简化这些类。

基本上我有一个基类BaseClass,其中两个属性引用了一个扩展BaseClass =>的类。 ClassA

如果我删除ClassB,它会起作用 如果我删除两个引用中的一个,它就可以工作 当我不从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();
        }
    }
}

我总是得到这个例外“序列包含多个元素”

如何让它发挥作用?

以下是Exception的堆栈跟踪:

   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()

1 个答案:

答案 0 :(得分:0)

现在已在最新版本的EF中修复: http://entityframework.codeplex.com/workitem/546