/// <summary> /// 添加指定组织的联系人 /// </summary> public static void AddContactWithOrganizer() { Console.Clear(); Console.WriteLine("组织列表"); var newO = new Organizer(); var newC = new Contact(); using (var context = new TryContext()) { var olist = from oname in context.Organizers select oname; foreach (var o in olist) { Console.WriteLine("ID:" + o.OrganizerId + " Name:" + o.Name); } } Console.WriteLine("请参照上方列表输入要创建联系人的组织ID后按Enter"); var oId = Convert.ToInt16(Console.ReadLine()?.Trim()); var exists = CheckForExistingOrganizerId(oId); if (exists) { Console.WriteLine("输入联系人姓名后按Enter"); var cName = Console.ReadLine(); Console.WriteLine("输入联系人电话后按Enter"); var cPhone = Console.ReadLine(); newC.Name = cName; newC.Phone = cPhone; newC.OrganizerId = oId; using (var context = new TryContext()) { Console.WriteLine("\r\n正在尝试保存..."); context.Contacts.Add(newC); var i = context.SaveChanges(); if (i == 1) { Console.WriteLine("添加联系人成功\r\n按任意键继续..."); Console.ReadKey(); } } } else { Console.WriteLine("\r\n你输入的组织不存在,请重新输入\r\n按任意键继续..."); Console.ReadKey(); } }
/// <summary> /// 添加指定组织的活动 /// </summary> public static void AddEventWithOrganizer() { Console.Clear(); Console.WriteLine("组织列表"); var newO = new Organizer(); var newE = new Event(); using (var context = new TryContext()) { var olist = from oname in context.Organizers select oname; foreach (var o in olist) { Console.WriteLine("ID:" + o.OrganizerId + " Name:" + o.Name); } } Console.WriteLine("请参照上方列表输入要创建活动的组织ID后按Enter"); var oId = Convert.ToInt16(Console.ReadLine()?.Trim()); var exists = CheckForExistingOrganizerId(oId); if (exists) { Console.WriteLine("输入活动名称后按Enter"); var eName = Console.ReadLine(); newE.Name = eName; newE.OrganizerId = oId; using (var context = new TryContext()) { Console.WriteLine("\r\n正在尝试保存..."); context.Events.Add(newE); var i = context.SaveChanges(); if (i == 1) { Console.WriteLine("添加活动成功\r\n按任意键继续..."); Console.ReadKey(); } } } else { Console.WriteLine("\r\n你输入的活动所属组织不存在,请重新输入\r\n按任意键继续..."); Console.ReadKey(); } }
/// <summary> /// 添加组织 /// </summary> public static void AddOrganizer() { Console.Clear(); ConsoleKeyInfo cki; string result; var cont = false; var newO = new Organizer(); var oName = ""; WriteHeader("添加新组织"); Console.WriteLine("输入组织名称后按Enter"); oName = Console.ReadLine(); newO.Name = oName; var exists = CheckForExistingOrganizer(newO.Name); if (exists) { Console.WriteLine("\r\n你输入的组织已经存在,请重新输入\r\n按任意键继续..."); Console.ReadKey(); } else { using (var context = new TryContext()) { Console.WriteLine("\r\n正在尝试保存..."); context.Organizers.Add(newO); var i = context.SaveChanges(); if (i == 1) { Console.WriteLine("添加组织成功\r\n按任意键继续..."); Console.ReadKey(); } } } }
/// <summary> /// 添加种子数据 /// </summary> private static void AddSeed() { Console.Clear(); Console.WriteLine("准备添加种子数据..."); using (var context = new TryContext()) { var org = new Organizer { Name = "胖才" }; context.Organizers.Add(org); var evt = new Event { Name = "扒蒜大赛" }; org.Events.Add(evt); var con = new Contact { Name = "江崟才" }; context.SaveChanges(); } Console.WriteLine("\r\n添加种子数据完成!"); Console.WriteLine("\r\n按任意键继续..."); Console.ReadKey(); }