Ale*_*fel 6 c# entity-framework ef-migrations
在使用Entity Framework 4.3的Code First Migrations时,我得到了几个未处理的异常.
数据库上下文:
public class MyAppContext : DbContext
{
public DbSet<Branch> Branches { get; set; }
public MyAppContext()
{ }
}
Run Code Online (Sandbox Code Playgroud)
实体:
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; }
}
Run Code Online (Sandbox Code Playgroud)
数据库初始化程序:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
我使用以下命令将Entity Framework 4.3安装到我的DAL项目和MVC项目中:
Install-Package EntityFramework
我已将MVC项目设置为启动项目,并使用数据库上下文和初始化程序对DAL项目执行以下命令:
PM>启用 - 迁移--Verbose
使用NuGet项目'Ckms.KeyManagement.Managers'.搜索上下文类型时出错(指定-Verbose以查看异常详细信息).System.Data.Entity.Migrations.Design.ToolingException:无法加载一个或多个请求的类型.检索LoaderExceptions属性以获取更多信息.at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)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)
{ }
}
Run Code Online (Sandbox Code Playgroud)
对Add-Migration和Update-Database命令抛出这些异常:
PM>添加迁移TestEFMigrationsColumn -Verbose
使用NuGet项目'Ckms.KeyManagement.Managers'.使用StartUp项目''.System.Reflection.TargetInvocationException:调用目标抛出了异常.---> System.ArgumentException:参数不正确.(来自HRESULT的异常:0x80070057(E_INVALIDARG))---内部异常堆栈跟踪结束---在System.RuntimeType.InvokeDispMethod(String name,BindingFlags invokeAttr,Object target,Object [] args,Boolean [] byrefModifiers,Int32 culture ,String [] namedParameters)位于System.RuntimeType.InvokeMember(String name,BindingFlags bindingFlags,Binder binder,Object target,Object [] providedArgs,ParameterModifier [] modifiers,CultureInfo culture,String [] namedParams),位于System.Management.Automation. ComMethod.InvokeMethod(PSMethod方法,Object []参数)调用的目标抛出了异常.
更新数据库:
PM>更新 - 数据库-Verbose
使用NuGet项目'Ckms.KeyManagement.Managers'.使用StartUp项目''.System.Reflection.TargetInvocationException:调用目标抛出了异常.---> System.ArgumentException:参数不正确.(来自HRESULT的异常:0x80070057(E_INVALIDARG))---内部异常堆栈跟踪结束---在System.RuntimeType.InvokeDispMethod(String name,BindingFlags invokeAttr,Object target,Object [] args,Boolean [] byrefModifiers,Int32 culture ,String [] namedParameters)位于System.RuntimeType.InvokeMember(String name,BindingFlags bindingFlags,Binder binder,Object target,Object [] providedArgs,ParameterModifier [] modifiers,CultureInfo culture,String [] namedParams),位于System.Management.Automation. ComMethod.InvokeMethod(PSMethod方法,Object []参数)调用的目标抛出了异常.
有任何想法吗?错误消息并不真正有用.我已经尝试过使用和不使用现有数据库的Nuget命令.
Mar*_*cin 12
如果您使用单独的库进行数据访问,则需要在运行查询时提供它的名称:
添加迁移-StartUpProjectName"您的DAL项目"MyNewMigration
Update-Database -StartUpProjectName"你的DAL项目"-Verbose
归档时间: |
|
查看次数: |
23781 次 |
最近记录: |