szab.kel
szab.kel

Reputation: 2526

How to add migrations for Entity Framework in a Xamarin Forms project?

I am trying to get the EF migrations to work in a Xamarin Forms project. I have a DbContext for a SQLite database:

    public class TestDbContext : DbContext
    {
        public DbSet<Item> Items { get; set; }

        public TestDbContext()
        {
            SQLitePCL.Batteries_V2.Init();

            this.Database.EnsureCreated();
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            string dbPath = Path.Combine(FileSystem.AppDataDirectory, "testDb.db3");

            optionsBuilder
                .UseSqlite($"Filename={dbPath}");
        }
    }

I installed the Microsoft.EntityFrameworkCore.Tools nuget for the standard library, but it did not work. Starting from this, I added a Dummy .NET Core project.

  1. I referenced the .NET Standard library (2.1) in the Dummy (Xamarin Forms project)
  2. I installed the Microsoft.EntityFrameworkCore.Sqlite and Microsoft.EntityFrameworkCore.Tools nuget packages into the dummy project as well
  3. I implemented an IDesignTimeDbContextFactory in the .net standard library like this:
    public class DesignTimeFactory : IDesignTimeDbContextFactory<TestDbContext>
    {
        public TestDbContext CreateDbContext(string[] args)
        {
            return new TestDbContext();
        }
    }

When I run the command PM> Add-Migration InitialCreate -P XamarinEFTest -S EFDummy I get an exception:

Build started...
Build succeeded.
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> Xamarin.Essentials.NotImplementedInReferenceAssemblyException: This functionality is not implemented in the portable version of this assembly. You should reference the NuGet package from your main application project in order to reference the platform-specific implementation.
   at Xamarin.Essentials.FileSystem.get_PlatformAppDataDirectory()
   at Xamarin.Essentials.FileSystem.get_AppDataDirectory()
   at XamarinEFTest.TestDbContext.OnConfiguring(DbContextOptionsBuilder optionsBuilder) in C:\Users\szabk\source\repos\XamarinEFTest\XamarinEFTest\XamarinEFTest\TestDbContext.cs:line 24
   at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
   at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
   at Microsoft.EntityFrameworkCore.Infrastructure.Internal.InfrastructureExtensions.GetService[TService](IInfrastructure`1 accessor)
   at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
   at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.get_Dependencies()
   at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated()
   at XamarinEFTest.TestDbContext..ctor() in C:\Users\szabk\source\repos\XamarinEFTest\XamarinEFTest\XamarinEFTest\TestDbContext.cs:line 19
   at XamarinEFTest.DesignTimeFactory.CreateDbContext(String[] args) in C:\Users\szabk\source\repos\XamarinEFTest\XamarinEFTest\XamarinEFTest\DesignTimeFactory.cs:line 12
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContextFromFactory(Type factory, Type contextType)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass16_0.<FindContextFactory>b__1()
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Exception has been thrown by the target of an invocation.

I have VS 2019 16.8.4, I created the test xamarin project from the layout template with the slider menu.

What do I need to do?

Edit: Maybe I need to setup the Core project with a different SQLite driver and with a different DesignTimeFactory? But how would this work? I need the migrations to work in the phone in the end.

Upvotes: 0

Views: 839

Answers (1)

Michal Diviš
Michal Diviš

Reputation: 2206

The Xamarin.Essentials FileSystem.AppDataDirectory doesn't work on Windows/Mac, where you're trying to create the migration.

Use this sqlite database path for creating the migrations:

optionsBuilder.UseSqlite($"Data Source=migrations.db3");

This path is relative and you'll end up with a migrations.db3 file created in your project folder. Just ignote the .db file, it's only purpose was to create the migration.

EDIT: I've been using this DbContext in my app and it works just fine.

public class MyDbContext : DbContext
{
    private string _databasePath;

    public DbSet<MyEntity> Issues { get; set; }

    [Obsolete("Don't use this for production. This is only for creating migrations.")]
    public MyDbContext() : this("nothing.db")
    {

    }

    public MyDbContext(string dbPath)
    {
        _databasePath = dbPath;
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
        {
            optionsBuilder.UseSqlite($"Data Source={_databasePath}");
        }
    }
}

Upvotes: 1

Related Questions