Using Fody Costura with Entity Framework Core migrations

北战南征 提交于 2021-01-07 12:58:56

问题


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.


回答1:


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.




回答2:


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>



回答3:


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.



来源:https://stackoverflow.com/questions/61396138/using-fody-costura-with-entity-framework-core-migrations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!