Xamarin.Android项目中使用EF Core创建迁移遇问题求助
Hey there, let's work through those frustrating migration errors you're facing with your Xamarin.Android project and SQLite. I've dealt with this exact scenario before, so here's a clear breakdown of what's going wrong and how to fix it:
Why These Errors Happen
EF Core's migration tools (whether using the dotnet ef CLI or Package Manager Console) don't support running directly against Xamarin.Android projects. The MonoAndroid framework isn't compatible with the tooling's requirements, which is why you see both:
- The "No executable found matching command 'dotnet-ef'" error when trying the CLI
- The "Startup project targets framework 'MonoAndroid'" error in Package Manager Console
The solution is to use a compatible auxiliary project to handle migration generation, then apply those migrations to your Xamarin app.
Step-by-Step Fix
1. Create an Auxiliary .NET Project
Add a new project to your solution that uses a framework EF Core tools support:
- Choose either a Class Library (.NET Core 3.1+ / .NET 5/6/7+) or a Console App (.NET Core 3.1+ / .NET 5/6/7+). This project only exists to generate migrations—no need to build any actual functionality here.
2. Install Required NuGet Packages
Make sure this auxiliary project has the same EF Core version as your Xamarin.Android project (version mismatches cause weird bugs!):
- Install
Microsoft.EntityFrameworkCore.Sqlite - Install
Microsoft.EntityFrameworkCore.Tools
If you plan to use the dotnet ef CLI, also ensure you have the global tool installed (or update it):
# Install if you haven't already dotnet tool install --global dotnet-ef # Update to match your EF Core version if needed dotnet tool update --global dotnet-ef
3. Connect Your DbContext to the Auxiliary Project
You need the EF Core tools to access your DbContext class. The cleanest way is to:
- Move your
DbContextand entity classes into a shared Class Library (both your Xamarin project and auxiliary project will reference this library). - If you don't want to restructure, either copy your
DbContextcode to the auxiliary project, or have the auxiliary project reference your Xamarin project (note: referencing Xamarin can cause dependency bloat, so the shared library is better).
To let EF Core tools find your DbContext at design time, add a factory class to the auxiliary project:
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Design; namespace MigrationHelperProject { public class YourDbContextFactory : IDesignTimeDbContextFactory<YourDbContext> { public YourDbContext CreateDbContext(string[] args) { var options = new DbContextOptionsBuilder<YourDbContext>() .UseSqlite("Data Source=placeholder.db") // Just a dummy connection string for migration generation .Options; return new YourDbContext(options); } } }
4. Generate Migrations
Option A: Package Manager Console (Visual Studio)
- Set your auxiliary project as the Startup Project in Solution Explorer.
- In the Package Manager Console, set the "Default Project" dropdown to your auxiliary project.
- Run the migration command:
Add-Migration InitialCreate
Option B: dotnet CLI
- Open a command line and navigate to your auxiliary project's directory.
- Run the migration command (adjust paths if your DbContext is in a shared/Xamarin project):
dotnet ef migrations add InitialCreate
5. Apply Migrations to Your Xamarin.Android App
- The migration files will be generated in your auxiliary project. Copy these files to your Xamarin.Android project (or leave them in the shared library if you used that setup).
- In your Xamarin app's code, when initializing your DbContext, call
Migrate()to apply the migrations to the device's SQLite database:using (var context = new YourDbContext()) { context.Database.Migrate(); // This applies all pending migrations }
Quick Fixes for Specific Errors
- "No executable found matching command 'dotnet-ef'": Ensure you've installed the global
dotnet-eftool (see step 2) and are running the command from your auxiliary project's directory (not the Xamarin project). - "Startup project targets framework 'MonoAndroid'": Double-check that your auxiliary project is set as the startup project and default project when running
Add-Migration.
内容的提问来源于stack exchange,提问作者Mathias Rönnlund

