Search code examples
c#wpfcode-first

Initialize database in code-first SQLite


Config that I set connectionstring to:

<connectionStrings>
    <add name="default" connectionString="data source=database\data.db;" providerName="System.Data.SQLite" />
</connectionStrings>

But in myDbContext I change my database location to :

base.Database.Connection.ConnectionString = @"data source=" + AppVariable.myPath + @"database\data.db;";

After that when my app launch my tables are not created, where is the problem?


Solution

  • extract from my code with nuget package...no need of connection string and datasource...

    using SQLite;
    
    public string SQLitePath => Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "xxxxx.sqlite");
    
    
    try
            {
                using (var db = new SQLiteConnection(SQLitePath) { Trace = IsDebug })
                {
                    List<TargetModel> test = db.Table<TargetModel>().Select(p => p).ToList();
                    return test;
                }
            }
            catch (Exception ex)
            {
                BusinessLogger.Manage(ex);
                return null;
            }