c# - Unable to create migrations after upgrading to ASP.NET Core 2.0 -
after upgrading asp.net core 2.0, can't seem create migrations anymore.
i'm getting
"an error occurred while calling method 'buildwebhost' on class 'program'. continuing without application service provider. error: 1 or more errors occurred. (cannot open database "..." requested login. login failed. login failed user '...'"
and
"unable create object of type 'mycontext'. add implementation of 'idesigntimedbcontextfactory' project, or see https://go.microsoft.com/fwlink/?linkid=851728 additional patterns supported @ design time."
the command ran $ dotnet ef migrations add initialcreate --startup-project "..\web"
(from project/folder dbcontext).
connection string: "server=(localdb)\\mssqllocaldb;database=database;trusted_connection=true;multipleactiveresultsets=true"
this program.cs
public class program { public static void main(string[] args) { buildwebhost(args).run(); } public static iwebhost buildwebhost(string[] args) => webhost.createdefaultbuilder(args) .usestartup<startup>() .build(); }
you can add class implements idesigntimedbcontextfactory inside of web project.
here sample code:
public class designtimedbcontextfactory : idesigntimedbcontextfactory<codingblastdbcontext> { public codingblastdbcontext createdbcontext(string[] args) { iconfigurationroot configuration = new configurationbuilder() .setbasepath(directory.getcurrentdirectory()) .addjsonfile("appsettings.json") .build(); var builder = new dbcontextoptionsbuilder<codingblastdbcontext>(); var connectionstring = configuration.getconnectionstring("defaultconnection"); builder.usesqlserver(connectionstring); return new codingblastdbcontext(builder.options); } }
then, navigate database project , run following command line:
dotnet ef migrations add initialmigration -s ../web/
dotnet ef database update -s ../web/
-s stands startup project , ../web/ location of web/startup project.
Comments
Post a Comment