Proper way to set mutiple AddDbContext() options?

Multi tool use
Multi tool use


Proper way to set mutiple AddDbContext() options?



I have my site solution split into multiple projects, one of which has the classes for the Database. When I tried to generate the first migration I get the following error:



Entity Framework Core 2.0.3-rtm-10026 initialized 'SiteDBContext'
using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options:
None



Change your migrations assembly by using DbContextOptionsBuilder. E.g.
options.UseSqlServer(connection, b =>
b.MigrationsAssembly("")). By default, the migrations
assembly is the assembly containing the DbContext.



Change your target project to the migrations project by using the
Package Manager Console's Default project drop-down list, or by
executing "dotnet ef" from the directory containing the migrations
project.



Your target project '' doesn't match your migrations
assembly ''. Either change your target project or change
your migrations assembly.



Change your migrations assembly by using DbContextOptionsBuilder. E.g.
options.UseSqlServer(connection, b =>
b.MigrationsAssembly("")). By default, the migrations
assembly is the assembly containing the DbContext. Change your target
project to the migrations project by using the Package Manager
Console's Default project drop-down list, or by executing "dotnet ef"
from the directory containing the migrations project.



I want to add thie MigrationsAssembly option along side my connection string command but can't figure out exactly how to do it.


MigrationsAssembly



My current code is:


public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDbContext<SiteDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("FFInfoDB")));
}



I tried:


public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDbContext<SiteDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("FFInfoDB"), options.MigrationsAssembly("FFInfo.DAL")));
}



But I get the error



DbContextOptionsBuilder does not cainta a definition for MigrationsAssembly



What is the proper way to add in this second option?




1 Answer
1



You are quite close, but the second level of configuration is done through an Action<SqlServerDbContextOptionsBuider> and not through the original Action<DbContextOptionsBuilder> options parameter.


Action<SqlServerDbContextOptionsBuider>


Action<DbContextOptionsBuilder> options



So, use this:


public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDbContext<SiteDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("FFInfoDB"), sqlServerOptions => sqlServerOptions.MigrationsAssembly("FFInfo.DAL")));
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

mNgXbllorHwC9BN6DnhCG,L5y,7qW 4ey HIcUUn,T49 OltbCMc9HlRdyE iIH6t4ODUg caK
0w7yVM,9lzyEVf4rcUVwV 8iZ,7EilZvF P

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

Audio Livestreaming with Python & Flask