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

时间:2015-07-23 16:02:02

标签: c# .net entity-framework visual-studio-2012 ef-migrations

我无法在Visual Studio 2013项目上正常运行Code First迁移。我使用的是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 Package Manager控制台重新安装EntityFramework,以及删除Migrations文件夹,然后尝试重新启用&#34;重新启用&#34;迁移。

如果您需要任何其他信息,请告知我们,谢谢

编辑:添加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
        }
    }
}

4 个答案:

答案 0 :(得分:0)

如果要使用迁移,则需要将初始化程序更改为MigrateDatabaseToLatestVersion或NullDatabaseInitializer。 https://msdn.microsoft.com/en-us/data/jj591621.aspx#initializer

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我遇到这种类型的问题 可能是您正在尝试为数据库中的列导入空值。但该字段不接受空值。

答案 3 :(得分:-1)

如果选择了单个启动项目,请检入解决方案属性。