在使用代碼優先實體框架4.3遷移時,我收到了幾個未處理的異常。實體框架代碼優先遷移的例外
數據庫方面:
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();
}
}
我安裝實體框架4.3到我的DAL項目,並使用MVC項目:
安裝,包裝的EntityFramework
我已經設置了MVC項目爲啓動項目,並執行以下命令來與數據庫上下文和初始化的DAL項目:
PM>啓用的遷移-Verbose
使用NuGet項目'Ckms.KeyManagement.Managers'。 搜索上下文類型時出錯(指定-Verbose以查看異常詳細信息)。 System.Data.Entity.Migrations.Design.ToolingException:無法加載一個或多個請求的類型。檢索LoaderExceptions屬性以獲取更多信息。在在 System.Data.Entity.Migrations.Design.ToolingFacade.GetContextTypes()
System.Data.Entity.Migrations.Design.ToolingFacade.Run(跑壘員 澆道)在 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-遷移和更新數據庫命令:
PM>添加遷移TestEFMigrationsColumn -Verbose
使用NuGet項目 'Ckms.KeyManagement.Managers'。使用啓動項目''。 System.Reflection.TargetInvocationException:異常被調用的目標引發 。 ---> System.ArgumentException: 參數不正確。 (Exception from HRESULT:0x80070057 (E_INVALIDARG))---內部異常堆棧跟蹤結束---在 System.RuntimeType.InvokeDispMethod(String name,BindingFlags invokeAttr,Object target,Object [] args,Boolean [] byrefModifiers , Int32 culture,String [] namedParameters)at System.RuntimeType。InvokeMember(字符串名稱,的BindingFlags 的BindingFlags,粘結劑粘結劑,對象目標,對象[] providedArgs, ParameterModifier []改性劑,CultureInfo的文化,字符串[] namedParams)在 System.Management.Automation.ComMethod.InvokeMethod(PSMethod方法, Object [] arguments) 調用的目標拋出異常。
更新,數據庫:
PM>更新,數據庫-Verbose
使用的NuGet項目 'Ckms.KeyManagement.Managers'。使用啓動項目''。 System.Reflection.TargetInvocationException:異常被調用的目標引發 。 ---> System.ArgumentException: 參數不正確。 (Exception from HRESULT:0x80070057 (E_INVALIDARG))---內部異常堆棧跟蹤結束---在 System.RuntimeType.InvokeDispMethod(String name,BindingFlags invokeAttr,Object target,Object [] args,Boolean [] byrefModifiers , 的Int32培養,字符串[] namedParameters)在 System.RuntimeType.InvokeMember(字符串名稱,的BindingFlags 的BindingFlags,粘結劑粘結劑,對象目標,對象[] providedArgs, ParameterModifier []改性劑,CultureInfo的文化,字符串[] namedParams )在 System.Management.Automation.ComMethod.InvokeMethod(PSMethod方法, Object []自變量) 調用的目標拋出了異常。
任何想法?錯誤消息並不真正有用。我已經嘗試了使用和不使用現有數據庫的Nuget命令。
這並不能解決它我害怕。如果將StartUpProjectName參數添加到該命令,則會引發以下錯誤:System.Reflection.ReflectionTypeLoadException:無法加載一個或多個請求的類型。 – 2012-03-06 15:19:50
你有在dll配置適當的設置。我的意思是配置節,sql連接等。 – Marcin 2012-03-06 15:32:59
就是這樣!我不得不將sql連接添加到DAL dll的app.config。請注意,-StartupProjectName參數必須指向MVC/ui項目而不是DAL項目。謝謝你的幫助。 – 2012-03-06 17:01:28