【问题标题】:Entity-Framework Derived dbContext Configuration exception实体框架派生的 dbContext 配置异常
【发布时间】:2014-05-26 16:20:33
【问题描述】:

我制作了一个 DLL 以包含在 ASP (MVC) 项目中,在 DLL 中有一些“基本”功能,当然,我将它包含在其他 MVC 项目中。 问题是我现在有一个 EF - Code first 项目,它使用 Configuration 类进行迁移:

internal sealed class Configuration : DbMigrationsConfiguration<Project.Models.DbContextTest>
    {
        public Configuration()
        {
            //AutomaticMigrationsEnabled = true;
        }

        protected override void Seed(Project.Models.DbContextTest context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }

    }

当我使用此配置时,出现以下异常:

Sequence contains more than one matching element

关于以下功能:

return orderBy != null ? orderBy(query).ToList() : query.ToList();

功能不是问题,因为当我删除配置类时,一切正常。 在获取表列表的查询中引发异常,它只有一个主键,没有相关表(外键)。

配置类是空的,所以我想知道它在做什么让我的 Dll 坏了?

提前致谢, 斯蒂芬

【问题讨论】:

  • 有趣的问题。几个猜测。 1)项目中肯定只有一个配置类? 2)当您覆盖构造函数时,也许也值得调用基本构造函数?恐怕这两个问题都是纯粹的猜测!
  • 1) 是的,只有一个配置类。 2)我试过了,但这并没有解决问题。
  • 调试的时候调用出错的那一行,是不是调用了配置方法?另外,异常中是否有更多细节,可能是InnerException

标签: entity-framework exception dbcontext derived


【解决方案1】:

这是启动项目和进行手动迁移的组合。 启动项目时,它会自动迁移代码优先模型,这与手动迁移相结合会产生此错误/异常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-15
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    • 2017-05-14
    相关资源
    最近更新 更多