public async Task <IActionResult> Create(Dog dog) { db.Dogs.Add(dog); await db.SaveChangesAsync(); return(RedirectToAction("Index")); }
public async Task <IActionResult> Create(Cat cat) { db.Cats.Add(cat); await db.SaveChangesAsync(); return(RedirectToAction("Index")); }
public async Task <IActionResult> PutCat(Guid id, Cat cat) { if (id != cat.Id) { return(BadRequest()); } _context.Entry(cat).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CatExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
private static async System.Threading.Tasks.Task generateFakeData(CatContext db) { await db.Database.MigrateAsync(); //We need to ensure the latest Migration was added. This is different than EnsureDatabaseCreated. Cat catGary = new Cat() { CatId = 1, Name = "Gary", MeowsPerSecond = 5 }; Cat catJack = new Cat() { CatId = 2, Name = "Jack", MeowsPerSecond = 11 }; Cat catLuna = new Cat() { CatId = 3, Name = "Luna", MeowsPerSecond = 3 }; List <Cat> catsInTheHat = new List <Cat>() { catGary, catJack, catLuna }; if (await db.Cats.CountAsync() < 3) { await db.Cats.AddRangeAsync(catsInTheHat); await db.SaveChangesAsync(); } }
public override async void ViewDidLoad() { System.Diagnostics.Debug.WriteLine("ViewController.ViewDidLoad()"); base.ViewDidLoad(); // NOTE: See LinkDescription.xml, which describes how to get the Xamarin linker to preserve key types and // methods that Entity Framework Core uses only via reflection. Without such instructions to the linker, // EF Core code such as below would generate runtime errors when the linker is used; e.g. in builds that // are deployed to device or for publishing to the App Store. Unfortunately, such issues aren't immediately // evident when running a sample like this in the simulator, as the default Xamarin iOS project config has // linking disabled for the iPhoneSimulator target. var dbFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); var fileName = "Cats.db"; var dbFullPath = Path.Combine(dbFolder, fileName); try { using (var db = new CatContext(dbFullPath)) { await db.Database.MigrateAsync(); //We need to ensure the latest Migration was added. This is different than EnsureDatabaseCreated. Cat catGary = new Cat() { CatId = 1, Name = "Gary", MeowsPerSecond = 5 }; Cat catJack = new Cat() { CatId = 2, Name = "Jack", MeowsPerSecond = 11 }; Cat catLuna = new Cat() { CatId = 3, Name = "Luna", MeowsPerSecond = 3 }; List <Cat> catsInTheHat = new List <Cat>() { catGary, catJack, catLuna }; if (await db.Cats.CountAsync() < 3) { await db.Cats.AddRangeAsync(catsInTheHat); await db.SaveChangesAsync(); } var catsInTheBag = await db.Cats.ToListAsync(); foreach (var cat in catsInTheBag) { textView.Text += $"{cat.CatId} - {cat.Name} - {cat.MeowsPerSecond}" + System.Environment.NewLine; } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); } }
public async Task <IActionResult> OnPostAsync() { _db.Cats.Add(cat); await _db.SaveChangesAsync(); return(RedirectToPage("/Index")); }
protected async override void OnAppearing() { var dbFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments); var fileName = "Cats.db"; var dbFullPath = Path.Combine(dbFolder, fileName); try { using (var db = new CatContext(dbFullPath)) { await db.Database.MigrateAsync(); //We need to ensure the latest Migration was added. This is different than EnsureDatabaseCreated. Cat catGary = new Cat() { CatId = 1, Name = "Gary", MeowsPerSecond = 5 }; Cat catJack = new Cat() { CatId = 2, Name = "Jack", MeowsPerSecond = 11 }; Cat catLuna = new Cat() { CatId = 3, Name = "Luna", MeowsPerSecond = 3 }; List <Cat> catsInTheHat = new List <Cat>() { catGary, catJack, catLuna }; if (await db.Cats.CountAsync() < 3) { await db.Cats.AddRangeAsync(catsInTheHat); await db.SaveChangesAsync(); } var catsInTheBag = await db.Cats.ToListAsync(); textView.Text = ""; foreach (var cat in catsInTheBag) { textView.Text += $"{cat.CatId} - {cat.Name} - {cat.MeowsPerSecond}" + System.Environment.NewLine; } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); } }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } Order order = new Order(); // { // Name = Input.Name, // Address = Input.Address // }; order.Name = Input.Name; order.Address = Input.Address; order.Items = new Collection <Cat>(); foreach (KeyValuePair <int, bool> entry in Input.Items) { // do something with entry.Value or entry.Key if (entry.Value == true) { Cat item = _db.Cats.FirstOrDefault(t => t.ID == entry.Key); order.Items.Add(item); } } order.OwnerID = _UserManager.GetUserId(User); order.Status = "New"; order.Date = DateTime.Now; _db.Orders.Add(order); await _db.SaveChangesAsync(); return(RedirectToPage("/Index")); }
protected async override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); // Get our button from the layout resource, // and attach an event to it Button button = FindViewById <Button>(Resource.Id.MyButton); button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); }; TextView textView = FindViewById <TextView>(Resource.Id.TextView1); var dbFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); var fileName = "Cats.db"; var dbFullPath = Path.Combine(dbFolder, fileName); try { using (var db = new CatContext(dbFullPath)) { await db.Database.MigrateAsync(); //We need to ensure the latest Migration was added. This is different than EnsureDatabaseCreated. Cat catGary = new Cat() { CatId = 1, Name = "Gary", MeowsPerSecond = 5 }; Cat catJack = new Cat() { CatId = 2, Name = "Jack", MeowsPerSecond = 11 }; Cat catLuna = new Cat() { CatId = 3, Name = "Luna", MeowsPerSecond = 3 }; List <Cat> catsInTheHat = new List <Cat>() { catGary, catJack, catLuna }; if (await db.Cats.CountAsync() < 3) { await db.Cats.AddRangeAsync(catsInTheHat); await db.SaveChangesAsync(); } var catsInTheBag = await db.Cats.ToListAsync(); foreach (var cat in catsInTheBag) { textView.Text += $"{cat.CatId} - {cat.Name} - {cat.MeowsPerSecond}" + System.Environment.NewLine; } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); } }
public Task <int> SaveAsync() { return(_context.SaveChangesAsync()); }