Open Closed

SQLite Issue in Testing during data seeding #5510


User avatar
0
Merna created

Hello, I'm trying to use unit testing but faced an issue during seeding data as I'm using EF core but in testing, I'm using SQLite. the issue is that I'm using command * "SET IDENTITY_INSERT [dbo].[...] ON" which we have to use, but it doesn't exist in SQLite. Here is the Exception:

  • Volo.Abp.AbpInitializationException : An error occurred during the initialize Volo.Abp.Modularity.OnApplicationInitializationModuleLifecycleContributor phase of the module Readzoil.Dpa.DpaTestBaseModule, Readzoil.Dpa.TestBase, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: SQLite Error 1: 'near "SET": syntax error'.. See the inner exception for details. ---- Microsoft.Data.Sqlite.SqliteException : SQLite Error 1: 'near "SET": syntax error'.*

So is there a way to fix without commenting these data seedings??

If you're creating a bug/problem report, please include followings:

  • ABP Framework version: v7.3.0
  • UI type: MVC
  • DB provider: EF Core

4 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Are you using SQL code for seeding?

    If so, it's not possible, because it doesn't exist in SQLite

    You need to use repositories to seed data instead of SQL code

  • User Avatar
    0
    Merna created

    Hello, I know this but is there a way to solve this one cause I have to use sql code to seed a specific contributor. Like is there a way to know if this contributor is executed by migrator or testing project or is there a way to avoid a specific contributor from execution in testing project??!

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    You can try this:

    
    public class MyDatabaseOptions
    {
        public string DatabaseProvider { get; set; }
    
        public bool IsSqlLite()
        {
            return DatabaseProvider == "SqlLite";
        }
    }
    
    Configure<AbpDbContextOptions>(options =>
    {
        Configure<MyDatabaseOptions>(databaseOptions =>
        {
            databaseOptions.DatabaseProvider = "SqlServer";   
        });
        
        options.UseSqlServer();
    });
    
    public class MyDataSeedContributor
        : IDataSeedContributor, ITransientDependency
    {
        private readonly IIdentityUserRepository _userRepository;
        private readonly MyDatabaseOptions _myDatabaseOptions;
    
        public MyDataSeedContributor(IIdentityUserRepository userRepository, IOptions<MyDatabaseOptions> options)
        {
            _userRepository = userRepository;
            _myDatabaseOptions = options.Value;
        }
    
    
        public async Task SeedAsync(DataSeedContext context)
        {
            if (_myDatabaseOptions.IsSqlLite())
            {
                
            }
        }
    }
    
  • User Avatar
    0
    Merna created

    Hello, Thanks, it works fine.

Made with ❤️ on ABP v9.1.0-preview. Updated on December 26, 2024, 06:07