添加遷移時,asp.net核心和EF核心錯誤

asp.net-core entity-framework-core

在asp.net核心應用程序中,我正試圖遷移EF代碼拳頭。

我得到了以下錯誤,

在啟動類'WebAppHandOn.Startup'上調用方法'ConfigureServices'時發生錯誤。考慮使用IDbContextFactory在設計時覆蓋DbContext的初始化。錯誤:無法加載文件或程序集“Microsoft.AspNetCore.Routing,Version = 1.0.1.0,Culture = neutral,PublicKeyToken = adb9793829ddae60”或其依賴項之一。定位的程序集的清單定義與程序集引用不匹配。 (HRESULT異常:0x80131040)

在'ApplicationDbContext'上找不到無參數構造函數。要么將無參數構造函數添加到'ApplicationDbContext',要么在與'ApplicationDbContext'相同的程序集中添加'IDbContextFactory'的實現。

這是不同的代碼文件,

  1. 啟動類

    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddMvc();
    
        // Add EntityFramework's Identity support.
        services.AddEntityFramework();
    
        // Add ApplicationDbContext.
        services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
    }
    
  2. ApplicationDBContext類

    public class ApplicationDbContext : DbContext {
        #region Constructor
    
        public ApplicationDbContext(DbContextOptions options) : base(options)
        {
        }
        #endregion Constructor
    
        #region Methods
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
        }
        #endregion Methods
    
        #region Properties
        public DbSet<Item> Items { get; set; }
        public DbSet<Comment> Comments { get; set; }
        public DbSet<ApplicationUser> Users { get; set; }
        #endregion Properties
    }
    
  3. Project.json類

 {
      "dependencies": {
     "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Routing": "1.0.0",
    "Microsoft.AspNetCore.Authentication.JwtBearer": "1.0.0",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
    "Newtonsoft.Json": "9.0.1",
    "Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0",
    "Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
    "Microsoft.EntityFrameworkCore": "1.0.0",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
    "Microsoft.EntityFrameworkCore.Design": "1.0.0-preview2-final",
    "TinyMapper": "2.0.8"
  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"
  },

熱門答案

檢查yourproject.csproj文件,刪除任何PackageReference標記中的任何預覽版本,並使用manage NuGet包重新安裝穩定版本。希望有所幫助



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因