2016-12-30 219 views
0

在一個asp.net核心應用程序,我試圖爲EF代碼拳頭遷移。asp.net核心和EF核心錯誤,同時添加遷移

我得到了下面的錯誤,

An error occurred while calling method 'ConfigureServices' on startup class 'WebAppHandOn.Startup'. Consider using IDbContextFactory to override the initialization of the DbContext at design-time. Error: Could not load file or assembly 'Microsoft.AspNetCore.Routing, Version=1.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

No parameterless constructor was found on 'ApplicationDbContext'. Either add a parameterless constructor to 'ApplicationDbContext' or add an implementation of 'IDbContextFactory' in the same assembly as 'ApplicationDbContext'.

這裏有不同的代碼文件,

  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" 
 
    },

+0

有可能你正在調用新的ApplicationDbContext(); 因爲乍一看似乎一切都很正常。 – Woot

+0

發現此https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet 2/3的他們下來他們有這個問題的解決方案 – Woot

回答

0

檢查yourproject.csproj文件,在任何PackageReference標記刪除任何預覽版本,並重新安裝穩定的版本與管理的NuGet包。希望有幫助