EntityframeworkCore建立資料庫遷移時出現 Unable to create an object of type 'ApplicationDbContext'.
阿新 • • 發佈:2018-12-15
錯誤詳情:
這個錯誤曾經困擾了我整整一個下午,百度了好久好久.....也使用Google搜尋了一下,找到了一點相關的內容,但是還是沒有解決問題。最終通過bing搜尋到了這個問題的解決方案(果然親兒子。。。其實沒有什麼聯絡啦...)問題描述如下:
Add-Migration CreateIdentitySchema // 在PMC 中新增這行命令出現了以下錯誤
Unable to create an object of type 'ApplicationDbContext'. Add an implementation of 'IDesignTimeDbContextFactory<ApplicationDbContext>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.
你需要建立一個Data資料夾,然後在裡面建立一個名稱為:DesignTimeDbContextFactory 的類,類程式碼如下:
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext> { public ApplicationDbContext CreateDbContext(string[] args) { IConfigurationRoot configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json") .Build(); var builder = new DbContextOptionsBuilder(); builder.UseSqlServer("server=.;uid=sa;pwd=102489;database=IdentityDemo"); return new ApplicationDbContext(builder.Options); } }
引用的網址: