简体   繁体   中英

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;");

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM