【问题标题】:Enabling EF code first migrations V 4.3.1.0启用 EF 代码优先迁移 V 4.3.1.0
【发布时间】:2012-09-15 02:33:51
【问题描述】:

我曾参与过其他迁移项目,没有遇到任何问题。在我当前的项目中,我将迁移和模型项目分开。添加迁移甚至启用迁移(已创建配置类)时出现以下错误:

PM> enable-migrations -Force 找不到从 DbContext 派生的类 在当前项目中。将生成的配置类编辑为 指定启用迁移的上下文。 System.Reflection.ReflectionTypeLoadException:无法加载一个或 更多请求的类型。检索 LoaderExceptions 属性 了解更多信息。在 System.Reflection.RuntimeModule.GetTypes(RuntimeModule 模块)在 System.Reflection.RuntimeModule.GetTypes() 在 System.Reflection.Assembly.GetTypes() 在 System.Data.Entity.ModelConfiguration.Mappers.TypeMapper.<.ctor>b_1(Assembly a) 在 System.Linq.Enumerable.d_142.MoveNext() at System.Collections.Generic.List1.InsertRange(Int32 索引, IEnumerable1 collection) at System.Data.Entity.ModelConfiguration.Mappers.TypeMapper..ctor(MappingContext mappingContext) at System.Data.Entity.DbModelBuilder.MapTypes(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.RetryLazy2.GetValue(TInput 输入) 在 System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
在 System.Data.Entity.Internal.LazyInternalContext.get_CodeFirstModel()
在 System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext 上下文,XmlWriter 作家)在 System.Data.Entity.Migrations.Extensions.DbContextExtensions.c_DisplayClass1.b_0(XmlWriter w) 在 System.Data.Entity.Migrations.Extensions.DbContextExtensions.GetModel(Action`1 writeXml) 在 System.Data.Entity.Migrations.Extensions.DbContextExtensions.GetModel(DbContext 上下文)在 System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration 配置,DbContext usersContext)在 System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration 配置)在 System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration 迁移配置)在 System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.RunCore() 在 System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run() 无法加载一种或多种请求的类型。检索 LoaderExceptions 属性以获取更多信息。

谢谢

【问题讨论】:

  • 你是否在包管理器的下拉列表中选择了包含 DbContext 的项目?
  • 是的,我做到了。还是不行。

标签: c# entity-framework


【解决方案1】:

消息说您没有从 DbContext 派生的类,这意味着您需要定义一个。

类似这样的:

class MyContext: DbContext
{
   public DBSet<Some_Entity_Which_Represent_DB_table> SetName {get;set;}

   public MyContext() : base(here you can specify database with its name or connectionString or leave it empty)
   {}
}

【讨论】:

    猜你喜欢
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 2016-03-18
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多