Mocking IPrincipal in ASP.NET Core

The controller’s User is accessed through the HttpContext of the controller. The latter is stored within the ControllerContext. The easiest way to set the user is by assigning a different HttpContext with a constructed user. We can use DefaultHttpContext for this purpose, that way we don’t have to mock everything. Then we just use that … Read more

How do you enforce lowercase routing in ASP.NET Core?

For ASP.NET Core: Add one of the following lines to the ConfigureServices method of the Startup class: services.AddRouting(options => options.LowercaseUrls = true); or services.Configure<RouteOptions>(options => options.LowercaseUrls = true); Thanks to Skorunka for the answer as a comment. I thought it was worth promoting to an actual answer.

Include several references on the second level

.ThenInclude() will chain off of either the last .ThenInclude() or the last .Include() (whichever is more recent) to pull in multiple levels. To include multiple siblings at the same level, just use another .Include() chain. Formatting the code right can drastically improve readability. _dbSet .Include(tiers => tiers.Contacts).ThenInclude(contact => contact.Titre) .Include(tiers => tiers.Contacts).ThenInclude(contact => contact.TypeContact) .Include(tiers … Read more

Can I generate script of a migration with EF code first and .net core

As per EF documentation you can use Script-Migration command. If you want to just script all the migrations you can simply call it from Package Manager console like that. If you want to just script the changes from the last migration you can call it like this: Script-Migration -From <PreviousMigration> -To <LastMigration> Be sure to … Read more

How to resolve IOptions instance inside ConfigureServices?

If you need to resolve service using the service provider manually you can use this AddSingleton/AddScoped/AddTransient overload: // Works for AddScoped and AddTransient as well services.AddSingleton<IBarService>(sp => { var fooService = sp.GetRequiredService<IFooService>(); return new BarService(fooService); } If you really want to, you can build an intermediate service provider using the BuildServiceProvider() method on the IServiceCollection: … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)