public static async Task <List <ParkArea> > ImportFromJsonAsync(ParkContext db, string json) { List <ParkArea> parkAreas = JsonConvert.DeserializeObject <List <ParkArea> >(json); var existedParks = db.ParkAreas.Where(p => parkAreas.Select(q => q.Name).Contains(p.Name)); (await existedParks.ToListAsync()).ForEach(p => db.ParkAreas.Remove(p)); foreach (var parkArea in parkAreas) { foreach (var ps in parkArea.ParkingSpaces) { ps.ParkArea = parkArea; ps.Class ??= ""; ps.ID = 0; } foreach (var a in parkArea.Aisles) { a.ParkArea = parkArea; a.Class ??= ""; a.ID = 0; } foreach (var w in parkArea.Walls) { w.ParkArea = parkArea; w.Class ??= ""; w.ID = 0; } db.Add(parkArea); } await db.SaveChangesAsync(); return(parkAreas); }
/// <summary> /// 注册 /// </summary> /// <param name="db"></param> /// <param name="username"></param> /// <param name="password"></param> /// <param name="registTime">内部测试使用的时间</param> /// <returns>如果用户名已存在,返回null,否则返回注册后的用户对象</returns> public async static Task <LoginOrRegisterResult> RegisterAsync(ParkContext db, string username, string password, DateTime registTime) { if (await db.CarOwners.AnyAsync(p => p.Username == username)) { return(new LoginOrRegisterResult() { Type = LoginOrRegisterResultType.Existed }); } CarOwner carOwner = new CarOwner() { RegistTime = registTime, LastLoginTime = registTime, Username = username, Password = CreateMD5(username + password), Enabled = true, }; db.Add(carOwner); await db.SaveChangesAsync(); return(new LoginOrRegisterResult() { CarOwner = carOwner }); }
public virtual T Insert(T t) { try { _context.Add(t); _context.SaveChanges(); return(t); } catch (Exception) { throw; } }