1

My WPF program is using Entity Framewok Core 3.1.1 (code-first) with a SQLite database. If the database file does not exist on startup, the program calls context.Database.Migrate() to create one. Works great.

When I added Costura.Fody (using NuGet), that huge collection of DLLs disappeared and the program still works - until it needs to create a new database file. Then the Migrate() function fails with an error:

The type initializer for 'Microsoft.Data.Sqlite.SqliteConnection' threw an exception - The path is not of a legal form.

I have a similar issue if I try to use the Package Manager Console to add a migration manually - I get

Your startup project doesn't reference Microsoft.EntityFrameworkCore.Design

Removing the Fody Costura package causes everything to start working again. I haven't found anything on SO or elsewhere that references this issue, and Fody seems to be very popular, so I must be doing something dumb, but I don't know where to look.

Does anyone know how to get Fody Costura and EF Core migrations to coexist?

Many thanks.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ClarkG
  • 81
  • 9

3 Answers3

3

Thanks to Tronald for putting me on the right track. For anyone else who comes across this, for me the trick was to exclude all the SQLitePCLRaw DLLs like this:

<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
  <Costura >
    <ExcludeAssemblies>
      SQLitePCLRaw.*
    </ExcludeAssemblies>
  </Costura>
</Weavers>
ClarkG
  • 81
  • 9
0

Sqlite doesn't do well as an embedded resourse without a bit of tweaking. I know this is a common issue with Unit Tests as well when using Costura.Fody as specified in the docs.

A simple adjustment of the Weavers xml should fix the issue though. Have a look at Embedding Sqlite Files.

Tronald
  • 1,520
  • 1
  • 13
  • 31
0

ClarkG, oh my god... I am trying to solve it at least 15 hours of clear time. Thank you!

Your solution works on Migrate(), but doesn't work on add-migration xxx It s a little problem... I just uninstall fody every time when I create a new migration. And then install it again.

If I don't uninstall Fody then add-migration writes:

Your startup project 'InstaGTO' doesn't reference Microsoft.EntityFrameworkCore.Design. This package is required for the Entity Framework Core Tools to work. Ensure your startup project is correct, install the package, and try again.

Marek Schwarz
  • 350
  • 5
  • 13