private void SaveButton_Click(object sender, EventArgs e) { Logger.Log($"[{nameof(SaveButton_Click)}] {sender} {e} Click!"); // TODO 数据操作 var title = TitieEdit.Text; var content = ContentEdit.Text; Logger.Log($"[{nameof(SaveButton_Click)}] Titile: '{title}', Content: '{content}'"); using (var context = new NoteWinCore.Stores.AppDbContext()) { var note = new NoteCore.Entitys.Note { Id = Guid.NewGuid().ToString(), Title = title, Content = content, CreateTime = DateTime.Now, }; context.Add(note); var ws = context.Users.Where(a => a.Email.Contains("*****@*****.**")).FirstOrDefault(); context.NoteUserRelations.Add(new NoteCore.Entitys.NoteUserRelation { UserId = ws.Id, NoteId = note.Id }); context.SaveChanges(); } this.Close(); }
static void DbInit() { using (var context = new NoteWinCore.Stores.AppDbContext()) { context.Database.EnsureCreated(); if (context.Users.Any() || context.Notes.Any() || context.NoteUserRelations.Any()) { return; } var wagsn = new NoteCore.Entitys.User { Id = Guid.NewGuid().ToString(), NickName = "Wagsn", Email = "*****@*****.**", Password = "******" }; context.Users.AddRange(new List <NoteCore.Entitys.User> { wagsn }); var first = new NoteCore.Entitys.Note { Id = Guid.NewGuid().ToString(), Title = "第一篇笔记", Content = "这是第一篇笔记,开始你的记录之旅吧。", CreateTime = DateTime.Now, Deleted = false }; context.Notes.AddRange(new List <NoteCore.Entitys.Note> { first }); context.Add(new NoteCore.Entitys.NoteUserRelation { UserId = wagsn.Id, NoteId = first.Id }); context.SaveChanges(); } }