ASP.net on Mac

At work I have an ASP.net Core 2.1 web app I’m developing. At work we use Windows and Visual Studio 2017 to develop on with a local MS SQL Server as the database backend. But, I want to be able to work on the project away from the office and I only have a Mac laptop. So I’m trying to get it running on Mac, which should be possible since it’s .Net Core after all. There were a few things I needed to change, which turned out to be one line!

I first followed the instructions on an ASP.net Core tutorial to add the following to my Startup.cs:

services.AddDbContext<MvcMovieContext>(options =>
                  options.UseSqlite("Data Source=MvcMovie.db"));

Of course with the appropriate database context and SQLite filename for your project.

Then Visual Studio Code gave me some warnings about missing using directives so I followed the directions on the Entity Framework docs and ran from the web app directory:

dotnet add package Microsoft.EntityFrameworkCore.Sqlite

dotnet add package Microsoft.EntityFrameworkCore.Design

Now the code runs enough to browse to the index page! But… it breaks when I try to access a page that uses the database. We haven’t setup the database yet.

Run your migrations manually:

dotnet ef database update

In my case, I got an error about the connection string:

Format of the initialization string does not conform to specification starting at index 0.

I search around a bit more and found that instead I should have used “Filename=”DatabaseName.db”:

services.AddDbContext<MemberKioskContext>(options =>
                  options.UseSqlite("Filename=MyDatabase.db"));

Now when I go to a page that uses the database (rather than plain static output), I get errors telling me that SQLite couldn’t find certain tables. So I open DB Browser for SQLite and see that the database has actually been created, but is totally empty, I go and manually create the same db structure that I use in the “normal” SQL Server database. (Remembering that my primary development environment is Windows for this project.)

As you go through each page, you’ll set errors if columns don’t exist so you can just try and see what you’ve missed adding to the SQLite db.

Now I can develop on Mac just by switching the services.AddDbContext to the SQL Server or SQLite by commenting out one line or the other!

Posted by Anthony