Posts

Showing posts with the label C#

Make ‘dotnet ef‘ work with EF Core 2.0

Image
EF Core 2.0 changed the way the CLI looks for the DBContext to use. There are now two ways: Invoke BuildWebHost in Startup.cs Use the class implementing IDesignTimeDbContextFactory<T> BuildWebHost is called even if a class implements IDesignTimeDbContextFactory<T>. Solution for this problem: Rename BuildWebHost to e.g. _BuildWebHost (making it private isn’t enough). Next step: create the class DesignTimeDbContextFactory : public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext> { public ApplicationDbContext CreateDbContext(string[] args)    {     IConfigurationRoot configuration = new ConfigurationBuilder()           .SetBasePath(Directory.GetCurrentDirectory())           .AddJsonFile("appsettings.json")           .Build();       var builder = ne...

Debugging Lua scripts in VS Code using MoonSharp

Image
Using MoonSharp as Lua interpreter in a .NET application is quite easy, but the docs lack of a good explanation on how to debug the Lua scripts. MoonSharp provides a Visual Studio Code extension and a .NET package for a debug server. I’m going to show how to implement the server using a simple .NET Core Console application. I won’t explain much about Lua or MoonSharp itself, so I expect you have at least some knowledge on how to use MoonSharp. You can find my demo application on GitHub . Preparing the environment For using MoonSharp, we need to install these NuGet packages: MoonSharp MoonSharp.Debugger.VsCode In VS Code you have to install the MoonSharp Debug extension. Make sure to use the latest VS Code version – in an older version the current line in the debugger wasn’t highlighted. In the launch.json, you have to put the debugServer object inside the configuration. The official documentation is wrong here. Loading the script For this demo, I added just one simple gl...

Creating a generic Clone() method for dictionaries

I wanted to create a Clone method for dictionaries which returns the same type as the Dictionary on which the method was called: This would be quite easy if we would do an extension method for every type of dictionary, but I wanted to do this for all IDictionary implementations. Here is my solution: Since cloning works by creating a new instance with the source dictionary as constructor parameter, we have to use Activator.CreateInstance to create a new instance.