2017-05-03 62 views
5

我有一個問題,當我試圖訪問我PartsDbContext現場我得到以下錯誤:實體框架核心使用多個DbContexts

System.Data.SqlClient.SqlException: 'Invalid object name 'fieldName''

看來,這是因爲我想使我的PartsDbContext使用與我的ApplicationDbContext相同的數據庫,它與Identity一起使用。我需要知道如何設置第二個dbcontext以與使用/創建不同數據庫的EF內核一起工作。

我試圖創建第二個連接字符串,但讓我這個錯誤:

System.Data.SqlClient.SqlException: 'Cannot open database "PartsDb" requested by the login. The login failed. Login failed for user 'DESKTOP-4VPU567\higle'.'

這裏是我的代碼:

appsettings.json

"ConnectionStrings": { 
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-PrecisionCustomPC-b14db89e-86ad-4855-a17f-ac64a04339aa;Trusted_Connection=True;MultipleActiveResultSets=true", 
    "PartsConnection": "Server=(localdb)\\mssqllocaldb;Database=PartsDb" 
}, 
"Logging": { 
    "IncludeScopes": false, 
    "LogLevel": { 
     "Default": "Warning" 
    } 
} 

PartsDbContext。 cs

public class PartsDbContext : DbContext 
{ 
    public DbSet<PartsViewModels.Tower> Towers { get; set; } 
    public DbSet<PartsViewModels.Motherboard> Motherboards { get; set; } 

    public PartsDbContext(DbContextOptions<PartsDbContext> options) 
     : base(options) 
    { 
    } 
} 

Startup.cs

public void ConfigureServices(IServiceCollection services) 
{ 
    // Add framework services. 
    services.AddDbContext<ApplicationDbContext>(options => 
     options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); 

    services.AddIdentity<ApplicationUser, IdentityRole>() 
     .AddEntityFrameworkStores<ApplicationDbContext>() 
     .AddDefaultTokenProviders(); 

    services.AddEntityFramework() 
     .AddDbContext<PartsDbContext>(options => 
      options.UseSqlServer(Configuration.GetConnectionString("PartsConnection"))); 

    services.AddMvc(); 

    services.AddAuthorization(options => 
    { 
     options.AddPolicy("RequireAdminRole", policy => policy.RequireRole("Admin")); 
    }); 

    // Add application services. 
    services.AddTransient<IEmailSender, AuthMessageSender>(); 
    services.AddTransient<ISmsSender, AuthMessageSender>(); 
} 

AdminController.cs

[Authorize(Policy = "RequireAdminRole")] 
public class AdminController : Controller 
{ 
    private readonly PartsDbContext _context; 

    public AdminController(PartsDbContext context) 
    { 
     _context = context; 
    } 

    public IActionResult Index() 
    { 
     return View(); 
    } 

    public IActionResult Towers() 
    { 
     var model = _context.Towers.ToList(); 
     return View(model); 
    } 
} 

var model = _context.Towers.ToList();是其中所述錯誤被顯示出來。

再次。我想設置我的PartsDbContext以使用EF-Core將自動創建數據庫的方式與Entity Framework Core一起工作。

+0

你能TY更換'「PartsConnection」:「服務器=(的LocalDB )\\ mssqllocaldb; Database = PartsDb「'with'」PartsConnection「:」Server =(localdb)\\ mssqllocaldb; Database = PartsDb; Trusted_Connection = True「'? –

回答

7

我想通了。這主要是因爲我意外刪除了Identity使用的數據庫,我需要弄清楚如何恢復。

顯然我的連接字符串沒有問題。我只需要進入包管理器,然後鍵入以下命令順序:

  1. Add-Migration init -Context PartsDbContext
  2. Update-Database -Context PartsDbContext

我發現了這一點,因爲這是我必須做的就是我的ApplicationDbContext再次工作,事實證明,當您在Visual Studio中使用個人用戶身份驗證創建新的MVC Core Web應用程序時,將爲您完成此步驟。

所以基本上添加更多DbContexts的步驟是:

  1. 創建的DbContext類
  2. appsettings.json
  3. 的的DbContext添加到您的配置創建爲一個的DbContext連接字符串服務Startup.cs
  4. 在將使用它的控制器中設置DbContext。
  5. 打開包管理器並運行上面的2行。 (如果「-Context」不行試試「--context」
  6. 運行程序,讓EntityFrameworkCore把其餘的工作
+0

您是否使用多個數據庫的相同數據庫? ,如何分隔EFMigrationsHistory表中每個上下文的遷移? – dotnethaggis

+1

我正在使用多個數據庫 –

+0

如果在啓動時有一個環境條件確定哪個appsettings.json連接字符串用於給定上下文會怎麼樣?哪一個下午控制檯將要使用? – DiscipleMichael