Entity Framework Core 1.0 RTM 添加迁移失败

Entity Framework Core 1.0 RTM add migration failure

自从更新Core 1.0 RTM后,我现在无法更新数据库:

dotnet ef migrations add CreatedJobcards

我现在收到这个错误:

System.TypeLoadException: Could not load type 'Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions' from assembly 'Microsoft.Extensions.DependencyInjection, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

我的 project.json 看起来像这样:

{
  "webroot": "wwwroot",
  "version": "1.0.0-*",
  "dependencies": {
    "Kendo.Mvc": "2016.2.630",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Identity": "1.0.0",
    "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.Mvc.DataAnnotations": "1.0.0",
    "Microsoft.AspNetCore.Mvc.TagHelpers": "1.0.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0",
    "Microsoft.EntityFrameworkCore": "1.0.0",
    "Microsoft.EntityFrameworkCore.Relational": "1.0.0",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
    "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.0.0",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview1-final",
    "Microsoft.Extensions.Configuration.Abstractions": "1.0.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Configuration.UserSecrets": "1.0.0",
    "Microsoft.Extensions.DependencyInjection": "1.0.0",
    "Microsoft.Extensions.DependencyInjection.Abstractions": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0"
  },
  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": {
      "version": "1.0.0-preview1-final",
      "imports": [
        "portable-net45+win8+dnxcore50",
        "portable-net45+win8"
      ]
    }
  },
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "version": "1.0.0",
          "type": "platform"
        }
      }
    }
  },
  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "gcServer": true
  },
  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "appsettings.json",
      "web.config"
    ]
  },
  "scripts": {
    "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

更新: 同样的错误也出现在下一个命令中:

dotnet ef database update 0

dotnet ef migrations remove

更新 2:包括数据库上下文文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using mysite.com.Models;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.AspNetCore.Identity;

namespace mysite.com.Data
{
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser, Role, Guid>
    {

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            builder.Entity<ApplicationUser>(b =>
            {
                b.Property(u => u.Id).HasColumnName("UserId").HasDefaultValueSql("newsequentialid()");
            });

            builder.Entity<Role>(b =>
            {
                b.Property(u => u.Id).HasDefaultValueSql("newsequentialid()");
            });


            builder.Entity<Attachment>(entity =>
            {
                entity.Property(e => e.AttachmentID)
                    .HasDefaultValueSql("newsequentialid()")
                    .ValueGeneratedOnAdd();

                entity.Property(e => e.AdminOnly).HasDefaultValueSql("0");

                entity.Property(e => e.CreatedOn).HasColumnType("datetime");

                entity.Property(e => e.EntityName)
                    .IsRequired()
                    .HasMaxLength(255);

                entity.Property(e => e.ModifiedOn).HasColumnType("datetime");

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasMaxLength(255);
            });

            builder.Entity<Employee>(entity =>
            {
                entity.Property(e => e.EmployeeID)
                    .HasDefaultValueSql("newsequentialid()")
                    .ValueGeneratedOnAdd();

                entity.Property(e => e.CreatedOn).HasColumnType("datetime");

                entity.Property(e => e.DateOfBirth).HasColumnType("date");

                entity.Property(e => e.EmployeeNumber).HasMaxLength(50);

                entity.Property(e => e.ID_Passport_No).HasMaxLength(255);

                entity.Property(e => e.ModifiedOn).HasColumnType("datetime");

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasMaxLength(255);

                entity.Property(e => e.Position).HasMaxLength(255);

                entity.Property(e => e.Status).HasMaxLength(255);

                entity.Property(e => e.Surname).HasMaxLength(255);

                entity.HasOne(d => d.Organisation)
                    .WithMany(p => p.Employee)
                    .HasForeignKey(d => d.OrganisationID)
                    .HasConstraintName("FK_Employee_Organisation");

                entity.HasOne(d => d.TestCycle)
                    .WithMany(p => p.Employee)
                    .HasForeignKey(d => d.TestCycleID)
                    .OnDelete(DeleteBehavior.SetNull)
                    .HasConstraintName("FK_Employee_TestCycle");
            });

            builder.Entity<Examiner>(entity =>
            {
                entity.HasOne(a => a.User)
                .WithMany(b => b.Examiners);

                entity.Property(e => e.ExaminerID)
                    .HasDefaultValueSql("newsequentialid()")
                    .ValueGeneratedOnAdd();

                entity.Property(e => e.CompanyName).HasMaxLength(255);

                entity.Property(e => e.CompanyTelephone).HasMaxLength(255);

                entity.Property(e => e.CompanyURL).HasMaxLength(255);

                entity.Property(e => e.CreatedOn).HasColumnType("datetime");

                entity.Property(e => e.Facebook).HasMaxLength(255);

                entity.Property(e => e.FirstName).HasMaxLength(255);

                entity.Property(e => e.Google).HasMaxLength(255);

                entity.Property(e => e.Instagram).HasMaxLength(255);

                entity.Property(e => e.LastName).HasMaxLength(255);

                entity.Property(e => e.LinkedIn).HasMaxLength(255);

                entity.Property(e => e.Mobile).HasMaxLength(255);

                entity.Property(e => e.ModifiedOn).HasColumnType("datetime");

                entity.Property(e => e.Skype).HasMaxLength(255);

                entity.Property(e => e.Status).HasMaxLength(255);

                entity.Property(e => e.Twitter).HasMaxLength(255);
            });

            builder.Entity<Note>(entity =>
            {
                entity.Property(e => e.NoteID)
                    .HasDefaultValueSql("newsequentialid()")
                    .ValueGeneratedOnAdd();

                entity.Property(e => e.CreatedOn).HasColumnType("datetime");

                entity.Property(e => e.EntityName)
                    .IsRequired()
                    .HasMaxLength(255);

                entity.Property(e => e.ModifiedOn).HasColumnType("datetime");

                entity.Property(e => e.NoteText)
                    .IsRequired()
                    .HasColumnName("Note");
            });

            builder.Entity<Organisation>(entity =>
            {
                entity.HasOne(a => a.User)
                .WithMany(b => b.Organisations);

                entity.Property(e => e.OrganisationID)
                    .HasDefaultValueSql("newsequentialid()")
                    .ValueGeneratedOnAdd();

                entity.Property(e => e.Branch).HasMaxLength(255);

                entity.Property(e => e.BranchCode).HasMaxLength(255);

                entity.Property(e => e.CreatedOn).HasColumnType("datetime");

                entity.Property(e => e.GPSCoords).HasMaxLength(255);

                entity.Property(e => e.ModifiedOn).HasColumnType("datetime");

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasMaxLength(255);

                entity.Property(e => e.Status).HasMaxLength(50);

                entity.Property(e => e.Telephone).HasMaxLength(255);

                entity.Property(e => e.VATNo).HasMaxLength(255);

                entity.Property(e => e.WebSite).HasMaxLength(255);

            });

            builder.Entity<OrganisationContact>(entity =>
            {
                entity.Property(e => e.OrganisationContactID)
                    .HasDefaultValueSql("newsequentialid()")
                    .ValueGeneratedOnAdd();

                entity.Property(e => e.CreatedOn).HasColumnType("datetime");

                entity.Property(e => e.Email).HasMaxLength(255);

                entity.Property(e => e.ModifiedOn).HasColumnType("datetime");

                entity.Property(e => e.Name).HasMaxLength(255);

                entity.Property(e => e.Surname).HasMaxLength(255);

                entity.Property(e => e.Telephone).HasMaxLength(255);

                entity.Property(e => e.Type).HasColumnType("nchar(10)");

                entity.HasOne(d => d.Organisation)
                    .WithMany(p => p.OrganisationContact)
                    .HasForeignKey(d => d.OrganisationID)
                    .HasConstraintName("FK_OrganisationContact_Organisation");
            });

            builder.Entity<Polygraph>(entity =>
            {
                entity.Property(e => e.PolygraphID)
                    .HasDefaultValueSql("newsequentialid()")
                    .ValueGeneratedOnAdd();


                entity.Property(e => e.CreatedOn).HasColumnType("datetime");

                entity.Property(e => e.ModifiedOn).HasColumnType("datetime");

                entity.Property(e => e.NextTestDate).HasColumnType("date");

                entity.Property(e => e.Place).HasMaxLength(255);

                entity.Property(e => e.Reason).HasMaxLength(255);

                entity.Property(e => e.Status).HasMaxLength(255);

                entity.Property(e => e.TestResult).HasMaxLength(255);

                entity.HasOne(d => d.Examiner)
                    .WithMany(p => p.Polygraph)
                    .HasForeignKey(d => d.ExaminerID)

                    .HasConstraintName("FK_Polygraph_Examiner");

                entity.HasOne(d => d.Employee)
                    .WithMany(p => p.Polygraph)
                    .HasForeignKey(d => d.EmployeeID)
                    .OnDelete(DeleteBehavior.SetNull)
                    .HasConstraintName("FK_Polygraph_Employee");

                entity.HasOne(d => d.PolygraphType)
                    .WithMany(p => p.Polygraph)
                    .HasForeignKey(d => d.PolygraphTypeID)
                    .OnDelete(DeleteBehavior.SetNull)
                    .HasConstraintName("FK_Polygraph_PolygraphType");
            });
            builder.Entity<Photo>(entity =>
            {
                entity.Property(e => e.PhotoID)
                    .HasDefaultValueSql("newsequentialid()")
                    .ValueGeneratedOnAdd();
            });

            builder.Entity<TestCycle>(entity =>
            {
                entity.Property(e => e.Description).HasMaxLength(maxLength: 255);

                entity.Property(e => e.DisplayOrder).HasDefaultValueSql(sql: "0");
            });

            builder.Entity<PolygraphType>(entity =>
            {
                entity.Property(e => e.PolygraphTypeID)
                    .UseSqlServerIdentityColumn()
                    .ValueGeneratedOnAdd();
            });

            builder.Entity<Jobcard>(entity =>
            {
                entity.Property(e => e.JobcardID)
                   .HasDefaultValueSql(sql: "newsequentialid()")
                   .ValueGeneratedOnAdd();

                entity.Property(e => e.CreatedOn).HasColumnType(typeName: "datetime");

                entity.Property(e => e.BookingCount).IsRequired(required: true);

                entity.Property(e => e.ModifiedOn).HasColumnType(typeName: "datetime");

                entity.Property(e => e.Reason).HasMaxLength(maxLength: 255);

                entity.HasOne(d => d.Organisation)
                    .WithMany(p => p.Jobcards)
                    .HasForeignKey(d => d.OrganisationId)
                    .HasConstraintName(name: "FK_Jobcard_Organisation");

                entity.HasOne(d => d.Examiner)
                    .WithMany(p => p.Jobcards)
                    .HasForeignKey(d => d.ExaminerId)
                    .HasConstraintName(name: "FK_Jobcard_Examiner");
            });
            // Customize the ASP.NET Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);
        }





        public virtual DbSet<Attachment> Attachment { get; set; }
        public virtual DbSet<Employee> Employee { get; set; }
        public virtual DbSet<Examiner> Examiner { get; set; }
        public virtual DbSet<Note> Note { get; set; }
        public virtual DbSet<Organisation> Organisation { get; set; }
        public virtual DbSet<OrganisationContact> OrganisationContact { get; set; }
        public virtual DbSet<Polygraph> Polygraph { get; set; }
        public virtual DbSet<PolygraphType> PolygraphType { get; set; }
        public virtual DbSet<TestCycle> TestCycle { get; set; }
        public virtual DbSet<Photo> Photo { get; set; }
        public virtual DbSet<Jobcard> Jobcard { get; set; }
        public DbSet<ApplicationUser> ApplicationUser { get; set; }
    }
}

因为您使用的是 1.0.0 版(而不是 rc2),所以您的工具应该是 preview2 而不是 preview1。您也不再需要 imports。修复 project.json 后,我建议使用 nuget.exe locals -Clear all 清理 NuGet 缓存 作为旁注 - 您已将 publish-iis 配置为 运行 作为发布后脚本,但您的项目中没有此工具,因此在发布时会出现错误,因为 dotnet 不会找不到这个工具。您需要将 Microsoft.AspNetCore.IISIntegration.Tools 添加到您的工具部分(再次 preview2 而不是 imports