EntityFramework4.3のCodeFirstMigrationsを使用しているときに、いくつかの未処理の例外が発生します。
データベースコンテキスト:
public class MyAppContext : DbContext
{
public DbSet<Branch> Branches { get; set; }
public MyAppContext()
{ }
}
エンティティ:
public class Branch : IEntity<Guid>
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public bool Active { get; set; }
}
データベース初期化子:
public class MyAppInitializer : CreateDatabaseIfNotExists<MyAppContext>
{
protected override void Seed(MyAppContext context)
{
context.Branches.Add(new Branch() { Id = branchId, Name = "Acme", Description = "Acme", Active = true });
context.SaveChanges();
}
}
以下を使用して、EntityFramework4.3をDALプロジェクトとMVCプロジェクトにインストールしました。
インストール-パッケージEntityFramework
MVCプロジェクトをスタートアッププロジェクトとして設定し、データベースコンテキストと初期化子を使用してDALプロジェクトに対して次のコマンドを実行しました。
PM>有効-移行-詳細
NuGetプロジェクト'Ckms.KeyManagement.Managers'を使用します。コンテキストタイプの検索中にエラーが発生しました(例外の詳細を表示するには、-Verboseを指定してください)。System.Data.Entity.Migrations.Design.ToolingException:要求されたタイプの1つ以上をロードできません。詳細については、LoaderExceptionsプロパティを取得してください。System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)at System.Data.Entity.Migrations.Design.ToolingFacade.GetContextTypes()
at System.Data.Entity.Migrations.MigrationsCommands.FindContextToEnable()編集移行を有効にするコンテキストを指定するために生成された構成クラス。プロジェクトCkms.KeyManagement.Managersに対してコードファーストマイグレーションが有効になっています。
DbMigrationsConfiguration子クラスがDALプロジェクトに追加されます。DbContextのタイプを手動で追加し、自動移行を有効にした場合:
internal sealed class Configuration : DbMigrationsConfiguration<MyAppContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(MyAppContext context)
{ }
}
これらの例外は、Add-MigrationコマンドとUpdate-Databaseコマンドに対してスローされます。
PM> Add-Migration TestEFMigrationsColumn -Verbose
NuGetプロジェクト'Ckms.KeyManagement.Managers'を使用します。スタートアッププロジェクトの使用''。System.Reflection.TargetInvocationException:呼び出しのターゲットによって例外がスローされました。---> System.ArgumentException:パラメータが正しくありません。(HRESULTからの例外:0x80070057(E_INVALIDARG))---内部例外スタックトレースの終了--- System.RuntimeType.InvokeDispMethod(String name、BindingFlags invokeAttr、Object target、Object [] args、Boolean [] byrefModifiers、Int32culture 、String [] namedParameters)at System.RuntimeType.InvokeMember(String name、BindingFlags bindingFlags、バインダーバインダー、オブジェクトターゲット、Object [] ProvidedArgs、ParameterModifier []修飾子、CultureInfoカルチャー、String [] namedParams)atSystem.Management.Automation。 ComMethod.InvokeMethod(PSMethodメソッド、
データベースを更新する:
PM> Update-Database -Verbose
NuGetプロジェクト'Ckms.KeyManagement.Managers'を使用します。スタートアッププロジェクトの使用''。System.Reflection.TargetInvocationException:呼び出しのターゲットによって例外がスローされました。---> System.ArgumentException:パラメータが正しくありません。(HRESULTからの例外:0x80070057(E_INVALIDARG))---内部例外スタックトレースの終了--- System.RuntimeType.InvokeDispMethod(String name、BindingFlags invokeAttr、Object target、Object [] args、Boolean [] byrefModifiers、Int32culture 、String [] namedParameters)at System.RuntimeType.InvokeMember(String name、BindingFlags bindingFlags、バインダーバインダー、オブジェクトターゲット、Object [] ProvidedArgs、ParameterModifier []修飾子、CultureInfoカルチャー、String [] namedParams)atSystem.Management.Automation。 ComMethod.InvokeMethod(PSMethodメソッド、
何か案は?エラーメッセージはあまり役に立ちません。既存のデータベースがある場合とない場合でNugetコマンドを試しました。