实体框架启用-迁移空引用异常

本文关键字:引用 异常 迁移 框架 启用 实体 | 更新日期: 2023-09-27 18:04:12

我无法获得Code First迁移在我的Visual Studio 2013项目上正常工作。我使用的是EF 6.1和c#。Net 4.5。当我尝试运行Enable-MigrationsEnable-Migrations -ContextTypeName DataContext时,我得到以下错误:

PM> Enable-Migrations -ContextTypeName DataContext -Force
Checking if the context targets an existing database...
System.NullReferenceException: Object reference not set to an instance of an object.
   at Infrastructure.DataAccess.DataContext..ctor() in c:'Users'mblack'Documents'Visual Studio 2013'Projects'BluemoonApp'Infrastructure'DataAccess'DataContext.cs:line 16
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Data.Entity.Infrastructure.DbContextInfo.CreateInstance()
   at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo, Func1 resolver)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase)
   at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
   at System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
   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.ScaffoldInitialCreate(String language, String rootNamespace)
   at System.Data.Entity.Migrations.EnableMigrationsCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
Object reference not set to an instance of an object.

我不明白为什么这不起作用。我已经尝试通过Nuget包管理器控制台重新安装EntityFramework,以及删除Migrations文件夹,然后尝试"重新启用"迁移。

如果您还需要其他信息,请告诉我,谢谢。

编辑:添加DataContext.cs和DataContextInitializer.cs

namespace Infrastructure.DataAccess
{
    public class DataContext : DbContext
    {
        public DataContext()
            : base(ConfigurationManager.ConnectionStrings["BluemoonDev"].ConnectionString)
        {
            Configuration.ProxyCreationEnabled = false;
        }
        public virtual DbSet<Task> Tasks { get; set; }
        public virtual DbSet<Test> Tests { get; set; }
        public virtual DbSet<Core.Models.Configuration> Configurations { get; set; }
        public virtual DbSet<Subscriber> Subscribers { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //modelBuilder.Entity<Task>().HasKey(t => t.TaskId);

            base.OnModelCreating(modelBuilder);
        }
    }
}
namespace Infrastructure.DataAccess
{
    public class DataContextInitializer : DropCreateDatabaseIfModelChanges<DataContext>
    {
        protected override void Seed(DataContext context)
        {
            // . . . Creating lists for each Entity
            // . . . Adding those items to context and saving changes
        }
    }
}

实体框架启用-迁移空引用异常

如果你打算使用迁移,你需要将你的初始化器更改为MigrateDatabaseToLatestVersion或NullDatabaseInitializer。https://msdn.microsoft.com/en-us/data/jj591621.aspx初始值设定项

问题是您需要在上下文的构造函数中设置连接字符串

我就面临这种问题可能您正在尝试为数据库中的列导入空值。

选中单个启动项目时,检入解决方案属性