public void ClearHistory() { historyWords.Clear(); using (BiliMusicContext db = new BiliMusicContext()) { db.SearchHistorys.RemoveRange(db.SearchHistorys); db.SaveChangesAsync(); } }
/// <summary> /// 初始化单一实例应用程序对象。这是执行的创作代码的第一行, /// 已执行,逻辑上等同于 main() 或 WinMain()。 /// </summary> public App() { this.InitializeComponent(); this.Suspending += OnSuspending; using (var db = new BiliMusicContext()) { db.Database.Migrate(); } }
public void LoadHistory() { historyWords.Clear(); using (BiliMusicContext db = new BiliMusicContext()) { foreach (var item in db.SearchHistorys.OrderByDescending(x => x.datetime)) { historyWords.Add(item.content); } } }
private void SaveHistory(string keyword) { using (BiliMusicContext db = new BiliMusicContext()) { var search = db.SearchHistorys.FirstOrDefault(x => x.content.Equals(keyword)); if (search != null) { search.datetime = DateTime.Now; } else { db.SearchHistorys.Add(new SearchHistory() { content = keyword, datetime = DateTime.Now }); } db.SaveChangesAsync(); } }