Entity Framework Core Using multiple DbContexts

前端 未结 1 481
孤独总比滥情好
孤独总比滥情好 2020-11-29 21:52

I\'m having a problem that when I try to access a field in my PartsDbContext I get the following error:

System.Data.SqlClient.SqlException: \'Invalid

相关标签:
1条回答
  • 2020-11-29 22:20

    I figured it out. This mostly came about because I accidentally deleted the database that Identity was using and I needed to figure out how to get it back.

    Apparently there's nothing wrong with my connection string the way it is. I just needed to go into the package manager and type these commands in this order:

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

    I found this out because that is what I had to do to get my ApplicationDbContext working again and it turns out that this step is done for you when you create a new MVC Core Web Application in Visual Studio using Individual User Authentication.

    So basically the steps for adding more DbContexts is to:

    1. Create a DbContext Class
    2. Create a Connection string for that DbContext in appsettings.json
    3. Add the DbContext to your configured services in Startup.cs
    4. Setup the DbContext in the controllers that will use it.
    5. Open the package manager and run the 2 lines above. (if "-Context" doesn't work try "--context"
    6. Run your program and let EntityFrameworkCore take care of the rest.
    0 讨论(0)
提交回复
热议问题