在程序集EF6中找不到上下文类型 [英] No context type was found in the assembly EF6

查看:98
本文介绍了在程序集EF6中找不到上下文类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事基于nopCommerce的项目.为了更新自定义实体,我想启用EF迁移.所以我运行以下命令:

I'm working on a nopCommerce based project. In order to update custom entity I want to enable EF migrations. So I run following command:

Enable-Migrations -StartUpProjectName nop.web -ContextProjectName Nop.Plugin.Payments.Deposit -verbose

并得到错误:

Using StartUp project 'Nop.Web'.
System.Data.Entity.Migrations.Infrastructure.MigrationsException: No context type was found in the assembly 'Nop.Plugin.Payments.Deposit'.
   at System.Data.Entity.Utilities.TypeFinder.FindType(Type baseType, String typeName, Func`2 filter, Func`2 noType, Func`3 multipleTypes, Func`3 noTypeWithName, Func`3 multipleTypesWithName)
   at System.Data.Entity.Migrations.Design.ToolingFacade.GetContextTypeRunner.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   at System.Data.Entity.Migrations.Design.ToolingFacade.GetContextType(String contextTypeName)
   at System.Data.Entity.Migrations.EnableMigrationsCommand.FindContextToEnable(String contextTypeName)
   at System.Data.Entity.Migrations.EnableMigrationsCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
No context type was found in the assembly 'Nop.Plugin.Payments.Deposit'.

上下文类的定义如下:

public class DepositTransactionObjectContext : DbContext, IDbContext
{
    public DepositTransactionObjectContext(string nameOrConnectionString) : base(nameOrConnectionString) { }

    public DepositTransactionObjectContext() { }

    public IList<TEntity> ExecuteStoredProcedureList<TEntity>(string commandText, params object[] parameters) where TEntity : BaseEntity, new()
    {
        throw new System.NotImplementedException();
    }

    public IEnumerable<TElement> SqlQuery<TElement>(string sql, params object[] parameters)
    {
        throw new System.NotImplementedException();
    }

    public int ExecuteSqlCommand(string sql, bool doNotEnsureTransaction = false, int? timeout = null, params object[] parameters)
    {
        throw new System.NotImplementedException();
    }

    public void Detach(object entity)
    {
        throw new System.NotImplementedException();
    }

    public bool ProxyCreationEnabled { get; set; }
    public bool AutoDetectChangesEnabled { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new DepositTransactionMap());

        base.OnModelCreating(modelBuilder);
    }

    public string CreateDatabaseInstallationScript()
    {
        return ((IObjectContextAdapter)this).ObjectContext.CreateDatabaseScript();
    }

    public void Install()
    {
        //It's required to set initializer to null (for SQL Server Compact).
        //otherwise, you'll get something like "The model backing the 'your context name' context has changed since the database was created. Consider using Code First Migrations to update the database"
        Database.SetInitializer<DepositTransactionObjectContext>(null);

        Database.ExecuteSqlCommand(CreateDatabaseInstallationScript());
        SaveChanges();
    }

    public void Uninstall()
    {
        this.DropPluginTable("DepositTransaction");
    }

    public new IDbSet<TEntity> Set<TEntity>() where TEntity : BaseEntity
    {
        return base.Set<TEntity>();
    }
}

我创建了一个空项目,并在那里启用了迁移.然后,我复制并调整了Configuration.cs,使其看起来像这样:

I've created empty project and enabled migrations there. Then I've copied and adjusted Configuration.cs so it looks like this:

namespace Nop.Plugin.Payments.Deposit.Migrations
{
    using Data;
    using System.Data.Entity.Migrations;

    internal sealed class Configuration : DbMigrationsConfiguration<DepositTransactionObjectContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
        }

        protected override void Seed(DepositTransactionObjectContext context)
        {
        }
    }
}

但是,当我运行Add-Migration时,出现以下错误:

However when I run Add-Migration I get following error:

Using StartUp project 'Nop.Web'.
Using NuGet project 'Nop.Plugin.Payments.Deposit'.
System.Data.Entity.Migrations.Infrastructure.MigrationsException: No migrations configuration type was found in the assembly 'Nop.Plugin.Payments.Deposit'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration).
   at System.Data.Entity.Utilities.TypeFinder.FindType(Type baseType, String typeName, Func`2 filter, Func`2 noType, Func`3 multipleTypes, Func`3 noTypeWithName, Func`3 multipleTypesWithName)
   at System.Data.Entity.Migrations.Utilities.MigrationsConfigurationFinder.FindMigrationsConfiguration(Type contextType, String configurationTypeName, Func`2 noType, Func`3 multipleTypes, Func`3 noTypeWithName, Func`3 multipleTypesWithName)
   at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.FindConfiguration()
   at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Scaffold(String migrationName, String language, String rootNamespace, Boolean ignoreChanges)
   at System.Data.Entity.Migrations.AddMigrationCommand.Execute(String name, Boolean force, Boolean ignoreChanges)
   at System.Data.Entity.Migrations.AddMigrationCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
No migrations configuration type was found in the assembly 'Nop.Plugin.Payments.Deposit'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration).

我读了很多相同的问题,到处都是在Enable-Migrations cmdlet中指定错误项目的原因(没有定义上下文).但是,正如您所看到的,这不是我的情况.

I read a lot of same questions and everywhere the reason was wrong project specified in the Enable-Migrations cmdlet (not one having context defined). But it's not my case as you can see.

还有什么可能是原因?

推荐答案

我已经解决了这个问题.

Soooo I've managed to solve the problem.

为了进行调查,我下载了Entity Framework的源代码并检查了异常堆栈中提到的所有方法.在方法System.Data.Entity.Utilities.TypeFinder.FindType中,我找到了这样的指令:

For investigation I've downloaded sources of Entity Framework and checked all methods mentioned in exception stack. In the method System.Data.Entity.Utilities.TypeFinder.FindType I've found such instruction:

var types = _assembly.GetAccessibleTypes()
                                 .Where(t => baseType.IsAssignableFrom(t));

将我带到其中找到此内容的GetAccessibleTypes()方法:

Which lead me to GetAccessibleTypes() method where I found this:

return assembly.DefinedTypes.Select(t => t.AsType());

具有DefinedTypes是System.Reflection.Assembly类的属性,我试图通过将程序集加载到Powershell中并获取此属性来手动获取此属性:

Having DefinedTypes is a property of System.Reflection.Assembly class I've tried to get this property manually by loading my assembly in Powershell and getting this property:

$a = [System.Reflection.Assembly]::LoadFrom("P:\nopCommerce\Presentation\Nop.Web\Plugins\Payments.Deposit\Nop.Plugin.Payments.Deposit.dll")
$a.DefinedTypes

结果是空白,因为根本没有类型.

The result was emptyness as there were no types at all.

所以我试图以其他方式获取类型:

So I tried to get types in a different way:

$a.GetTypes()

结果是错误:

使用"0"参数调用"GetTypes"的异常:无法加载一个 或更多要求的类型.检索LoaderExceptions属性 以获得更多信息."

Exception calling "GetTypes" with "0" argument(s): "Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information."

当我检查LoaderException属性时,发现以下内容:

When I checked LoaderException property I've found following:

$Error[0].Exception.InnerException.LoaderExceptions

无法加载文件或程序集'Autofac,版本= 3.5.0.0, 文化=中性,PublicKeyToken = 17863af14b0044da'或其中之一 依赖关系.该系统找不到指定的文件.不能 加载文件或程序集'Nop.Services,版本= 3.8.0.0,文化=中性, PublicKeyToken = null"或其依赖项之一.系统无法 查找指定的文件.无法加载文件或程序集 'System.Web.Mvc,版本= 5.2.3.0,文化=中性, PublicKeyToken = 31bf3856ad364e35'或其依赖项之一.这 系统找不到指定的文件.无法加载文件或程序集 'System.Web.Mvc,版本= 5.2.3.0,区域性=中性, PublicKeyToken = 31bf3856ad364e35'或其依赖项之一.这 系统找不到指定的文件.无法加载文件或程序集 'System.Web.Mvc,版本= 5.2.3.0,文化=中性, PublicKeyToken = 31bf3856ad364e35'或其依赖项之一.这 系统找不到指定的文件.无法加载文件或程序集 'System.Web.Mvc,版本= 5.2.3.0,区域性=中性, PublicKeyToken = 31bf3856ad364e35'或其依赖项之一.这 系统找不到指定的文件.

Could not load file or assembly 'Autofac, Version=3.5.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da' or one of its dependencies. The system cannot find the file specified. Could not load file or assembly 'Nop.Services, Version=3.8.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified. Could not load file or assembly 'System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. Could not load file or assembly 'System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. Could not load file or assembly 'System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. Could not load file or assembly 'System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

.NET似乎试图加载所有程序集,我的程序集依赖于并且当然不能,因为它们不在同一个文件夹中,因为它们是共享的,位于其他位置,并且是由启动项目而不是由我的程序加载的.

It looked .NET tried to load all assemblies, my assembly depended on and of course could not as they are not in same folder because they are shared, located elsewhere and loaded by start up project, not by mine.

因此,我已将所有必需的程序集都复制到了与我相同的文件夹中,并尝试再次启用Enable-Migrations.这次执行时没有任何错误!

So I've copied all required assemblies to same folder as mine and tried to Enable-Migrations again. This time it was performed without any errors!

我问自己为什么启用迁移不将所有这些程序集本身加载,因为它考虑了启动项目.但这是个小问题.

I ask myself why wouldn't Enable-Migrations load all those assemblies itself as it takes start up project into consideration. But it's rather minor issue.

这篇关于在程序集EF6中找不到上下文类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆