Search code examples
c#sqliteasp.net-corexunitaspnetboilerplate

How to use real DB with SQLite in ABP framework?


I'm using Xunit for writing test cases and using ABP framework which is built on top of Asp.Net Core 2.0. I want to connect to my local DB instead of memory DB so that I can verify the records while running test cases.

public static void Register(IIocManager iocManager)
{
    RegisterIdentity(iocManager);

    var builder = new DbContextOptionsBuilder<MyCompanyDbContext>();

    var inMemorySqlite = new SqliteConnection("Data Source=:memory:");
    builder.UseSqlite(inMemorySqlite);

    inMemorySqlite.Open();

    iocManager.IocContainer.Register(
        Component
            .For<DbContextOptions<MyCompanyDbContext>>()
            .Instance(builder.Options)
            .LifestyleSingleton()
    );

    new MyCompanyDbContext(builder.Options).Database.EnsureCreated();
}

So to connect with my local DB I have made the below change, But its giving error. I'm using the same connection string in the application at other places and it works fine.

var inMemorySqlite = new SqliteConnection("Server=.\\SQLEXPRESS; Database=MyLocalDb; Trusted_Connection=True;");

Solution

  • You are trying to use a Non SQLite connection string with a SqliteConnection object.

    If the intention is connect to an actual db then the connection string should refer to an actual db file

    var optionsBuilder = new DbContextOptionsBuilder<MyCompanyDbContext>();
    var connectionString = "Data Source=MyRealLocalDb.db"; //use actual path
    optionsBuilder.UseSqlite(connectionString);
    
    //...
    

    Reference Configuring a DbContext