实体框架Core 1.0 RTM添加迁移失败

时间:2016-07-04 20:40:24

标签: asp.net-mvc entity-framework kendo-ui asp.net-core-1.0

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

  

dotnet ef迁移添加了CreatedJobcards

我现在收到此错误:

  

System.TypeLoadException:无法从程序集“Microsoft.Extensions.DependencyInjection,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = adb9793829ddae60”加载类型“Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions”。

我的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数据库更新0

     

dotnet ef迁移删除

更新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 :(得分:1)

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