【问题标题】:Entity Framework Core 1.0 RTM add migration failureEntity Framework Core 1.0 RTM 添加迁移失败
【发布时间】:2016-07-04 20:40:24
【问题描述】:

自从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; }
    }
}

【问题讨论】:

  • 你也可以显示你的 DbContext 吗?
  • @VolkanSeçkinAkbayır 我可以,但是看到应用程序构建没有问题,并且在运行 'dotnet ef database update 0' 时也会出现错误,我真的不认为这与 dbContext。但是,我会更新帖子以包含它
  • 因为你使用的是1.0.0版本的工具应该是preview2而不是preview1。您还应该从工具中删除导入。最后,在您解决这些问题后,我建议您使用 nuget.exe locals -Clear all 清理 NuGet 缓存
  • 您也将 publish-iis 工具配置为您的发布后脚本,但您尚未在 project.json 中包含此工具。您将在发布期间看到错误(但这是一个单独的问题)
  • 谢谢@Pawel。现在已经排序了。我不得不再次重做这个过程。

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-02-06
    • 1970-01-01
    • 2019-01-11
    • 2017-06-17
    • 2018-06-08
    • 1970-01-01
    • 1970-01-01
    • 2017-10-27
    • 2017-03-14
    相关资源
    最近更新 更多