UWP, SQLLite, EntityFramework and Code First

0

This one is driving me pretty insane. I am trying to develop a UWP app that targets Windows 10 Creators. I want to create a database using Code First techniques. Now I believe that means I have to use SQLLite.

So here we have may DataContext

public class DataContext : DbContext
{
    static DataContext()
    {
        using (var database = new DataContext())
        {
            database.Database.Migrate();
        }
    }

    protected override void OnConfiguring
        (DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite("Filename=CRM.db");
    }

    internal DbSet<EmailAddress> EmailAddress { get; set; }
    internal DbSet<PhoneNumbers> PhoneNumber { get; set; }
    internal DbSet<FaxNumbers> FaxNumber { get; set; }
    internal DbSet<NumberTypes> NumberType { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}

When I enter the command Add-Migration InitialCreate into the Package Manager Console I get the following error :-

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeInitializationException: The type initializer for 'Magic_CRM_WUA.Data.DataContext' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'sqlite3': This operation is only valid in the context of an app container. (Exception from HRESULT: 0x8007109A)
   at Microsoft.Data.Sqlite.Interop.NativeMethods.Sqlite3_sqlite3.sqlite3_open_v2(IntPtr filename, Sqlite3Handle& ppDb, Int32 flags, IntPtr vfs)
   at Microsoft.Data.Sqlite.Interop.NativeMethods.Sqlite3_sqlite3.open_v2(IntPtr filename, Sqlite3Handle& ppDb, Int32 flags, IntPtr vfs)
   at Microsoft.Data.Sqlite.Interop.NativeMethods.sqlite3_open_v2(String filename, Sqlite3Handle& ppDb, Int32 flags, String vfs)
   at Microsoft.Data.Sqlite.SqliteConnection.Open()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open()
   at Microsoft.EntityFrameworkCore.Storage.Internal.SqliteRelationalConnection.Open()
   at Microsoft.EntityFrameworkCore.Storage.Internal.SqliteDatabaseCreator.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade databaseFacade)
   at Magic_CRM_WUA.Data.DataContext..cctor() in D:\InHouseTfs\Magic CRM\Universal\Development\Magic CRM WUA\Magic CRM WUA\Data\DataContext.cs:line 12
   --- End of inner exception stack trace ---
   at Magic_CRM_WUA.Data.DataContext..ctor()
   --- End of inner exception stack trace ---
   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
   at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, Boolean nonPublic)
   at System.Activator.CreateInstance(Type type)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass12_3.<FindContextTypes>b__13()
   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)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_1.<.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.

Im pretty sure im missing something but I have no idea what.

Any help will be appreciated.

entity-framework
sqlite
uwp
code-first
asked on Stack Overflow Jun 25, 2017 by MagicWand • edited Jun 25, 2017 by magicandre1981

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0