2017-01-27 19 views
2

私はnopCommerceベースのプロジェクトで作業しています。カスタムエンティティを更新するために、EF移行を有効にしたいと思います。コンテキストクラスは、次のように定義されてアセンブリでコンテキスト・タイプが見つかりませんでした。EF6

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

私は空のプロジェクトを作成し、そこにマイグレーションを有効にした

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

とエラーが表示されます。だから私は、次のコマンドを実行します。 。私は、同じ質問をたくさん読み、

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

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) 
     { 
     } 
    } 
} 

Iアドインの移行を実行したときしかし、私は次のエラーを取得する:そして、私はそれは次のようになりますので、Configuration.csをコピーして調整してきましたその理由は、Enable-Migrationsコマンドレットで指定されたプロジェクトが間違っていたためです(コンテキストが定義されていない)。しかし、あなたが見ることができるように私の場合ではありません。

他に何が原因である可能性がありますか?

答えて

2

Soooo私は問題を解決できました。

調査のために、Entity Frameworkのソースをダウンロードし、例外スタックに記載されているすべてのメソッドをチェックしました。 DefinedTypesを持つ

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

:私はこれを見つけたGetAccessibleTypesに私を導く

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

()メソッド:メソッドSystem.Data.Entity.Utilities.TypeFinder.FindTypeで、私はこのような命令を見つけました私はPowerShellで私のアセンブリをロードすると、このプロパティを取得することにより、手動でこのプロパティを取得しようとしたSystem.Reflection.Assemblyクラスのプロパティです:

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

結果はEMPましたどのタイプも全くなかったからです。

は、だから私は、別の方法でタイプを取得しようとしました:

$a.GetTypes() 

結果がエラーだった:

$Error[0].Exception.InnerException.LoaderExceptions 
:私はLoaderExceptionプロパティをチェックすると

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

私は、次のことがわかりました

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はすべてのアセンブリを読み込もうとしましたが、私のアセンブリは依存していましたが、同じフォルダにないので共有できませんでした。

私はすべての必要なアセンブリを私と同じフォルダにコピーし、再度有効にすることを試みました。今回は何のエラーもなく実行されました!

私は自分自身に質問します。なぜなら、Enable-Migrationsは、プロジェクトの開始を考慮して、すべてのアセンブリをロードするのではないと思います。しかしそれはほんの軽微な問題です。

0

また、移行する場所を指定するには、-ProjectNameパラメーターを追加する必要があります。したがって、たとえば:

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

デフォルトでは、コンソールウィンドウで選択したプロジェクトにする必要があります。 https://coding.abel.nu/2012/03/ef-migrations-command-reference/

+0

私もそれを試しました。結果は同じです。 – Ralfeus

関連する問題