I am using the Microsoft.EntityFrameworkCore.Sqlit package as you can see in my .cproj file. I also wrote myself a ModelDBContext class that ist singelton and inherits from AbstractDBContex. Model Objects of type Stock can be stored in it. AbstractDBContext inherits from DBContext and implements OnConfiguring(). It also handles the initial implementation of the Stocks table.
My question:
can I tell SQLite to store the data persistently? I want them to be preserved after a restart.
I am a novice in the field. Am I using the ORM halfway correctly or is another approach recommended?
Here I show you my folder structure and the above mentioned classes:
folder structure of my WPF-Projekt
MyProjekt
|
|___Database
| |___analyser.sqlite
|
|___Views
| |___AddStock
| | |___AddStock.xaml (UserControl)
| | |___AddStock.xaml.cs
| |___StockBox
| |___StockBox.xaml (UserControl)
| |___StockBox.xaml.cs
|
|___ViewModels
| |___ViewModel.cs
|
|___Models
| |___Stock.cs
| |___ModelDBContext
|
|___App.xaml
|___App.xaml.cs
|___MainWindow.xaml
|___MainWindow.xaml.cs
|___RelayCommand.cs
analyser.cproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.0-preview.6.23329.4" />
<PackageReference Include="System.Data.SQLite" Version="1.0.118" />
</ItemGroup>
</Project>
AbstractDBContext
namespace analyser
{
public abstract class AbstractDBContext : DbContext
{
private string connectionString;
// Komplettes SQL-Verbindungsobjekt
private SQLiteConnection Connection;
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Verbindungszeichenfolge zur SQLite-Datenbank
SQLiteConnection.CreateFile("Database\\analyser.sqlite");
connectionString = "Data Source=Database\\analyser.sqlite";
this.Connection= new SQLiteConnection(connectionString);
optionsBuilder.UseSqlite(connectionString);
Init();
}
protected void Init(){
Connection.Open();
// Erstellen Sie die Tabelle "Stock" mit den Feldern "WKN" und "Titel"
string createTableQuery = @"CREATE TABLE IF NOT EXISTS Stocks (
WKN TEXT PRIMARY KEY,
Titel TEXT
);";
using (var command = new SQLiteCommand(createTableQuery, Connection))
{
command.ExecuteNonQuery();
}
Connection.Close();
}
}
}
ModelDBContext
namespace analyser
{
public class ModelDBContext : AbstractDBContext
{
// Private statische Instanz der ModelDBContext-Klasse
private static ModelDBContext instance;
public DbSet<Stock> Stocks { get; set; }
public static ModelDBContext Instance
{
get
{
if (instance == null)
{
instance = new ModelDBContext();
}
return instance;
}
}
}
}
Stock.cs
namespace analyser
{
public class Stock
{
[Key]
public string Wkn { get; set; }
public string Titel { get; set; }
}
}
Cutout of MainViewModel.cs
private ModelDBContext dbContext= ModelDBContext.Instance;
public void Save()
{
// Speichern Sie die Werte der Textboxen in das Stock-Objekt
stock.Titel = Titel;
stock.Wkn = Wkn;
Console.WriteLine($"Generate new Stock with Titel {stock.Titel} and WKN {stock.Wkn}");
ObservableStockList.Add(stock);
// Fügen Sie das Stock-Objekt zur Datenbank hinzu
dbContext.Stocks.Add(stock);
dbContext.SaveChanges();
}