コード例 #1
0
ファイル: Form1.cs プロジェクト: AlexSchaffhauser/NETProject
        private void planetBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            // Currently, the Entity Framework doesn’t mark the entities
            // that are removed from a navigation property (in our example the Products)
            // as deleted in the context.
            // The following code uses LINQ to Objects against the Local collection
            // to find all products and marks any that do not have
            // a Category reference as deleted.
            // The ToList call is required because otherwise
            // the collection will be modified
            // by the Remove call while it is being enumerated.
            // In most other situations you can do LINQ to Objects directly
            // against the Local property without using ToList first.
            foreach (var building in _context.Buildings.Local.ToList())
            {
                if (building.Planet == null)
                {
                    _context.Buildings.Remove(building);
                }
            }
            // save to data base
            _context.SaveChanges();

            // refresh data
            planetDataGridView.Refresh();
        }
コード例 #2
0
        // upgrade button clicked
        private void btn_Upgrade_Click(object sender, EventArgs e)
        {
            if (buildingsDataGridView.SelectedRows.Count != 0)
            {
                DataGridViewRow row = buildingsDataGridView.SelectedRows[0];

                int selectedBuildingId = (int)row.Cells[0].Value;

                var result = _context.Buildings.SingleOrDefault(b => b.BuildingId == selectedBuildingId);
                if (result != null)
                {
                    if (planetResource >= result.BuildingCost)
                    {
                        planetResource         -= result.BuildingCost;
                        tb_planetResources.Text = planetResource.ToString();
                        result.BuildingCost    *= 2;
                        result.BuildingLevel   += 1;

                        if (result.BuildingName.Equals("Factory"))
                        {
                            factoryLevel = result.BuildingLevel;
                        }
                        else
                        {
                            storageLevel = result.BuildingLevel;
                        }
                    }
                    _context.SaveChanges();
                    buildingsDataGridView.Refresh();
                }
            }
        }
コード例 #3
0
        private void btnAddPlanet_Click(object sender, EventArgs e)
        {
            //add new Planet
            var newPlanet = new Planet()
            {
                PlanetName = tbxPlanetName.Text
            };

            _context.Planets.Add(newPlanet);
            //Add Factory to Planet
            var factory = new Building()
            {
                BuildingName  = "Factory",
                BuildingCost  = 10,
                BuildingLevel = 1,
                BuildingId    = newPlanet.PlanetId
            };

            _context.Buildings.Add(factory);
            //Add Storage to Planet
            var storage = new Building()
            {
                BuildingName  = "Storage",
                BuildingCost  = 10,
                BuildingLevel = 1,
                BuildingId    = newPlanet.PlanetId
            };

            _context.Buildings.Add(storage);
            _context.SaveChanges();
            this.Close();
        }