示例#1
0
        public static void Seed()
        {
            var context = new ApplicationDbContext();

            var asia = new Continent()
            {
                Name = "Asia"
            };

            var europe = new Continent()
            {
                Name = "Europe"
            };

            context.Continents.Add(asia);
            context.Continents.Add(europe);
            context.SaveChanges();

            var china = new Country()
            {
                Continent = asia,
                Language = "chinese",
                Name = "China",
                Population = 2123452123
            };

            var romania = new Country()
            {
                Continent = europe,
                Language = "romanian",
                Name = "Romania",
                Population = 1000000
            };

            context.Countries.Add(romania);
            context.Countries.Add(china);

            var buku = new Town()
            {
                Name = "Bukuresht",
                Country = romania
            };

            var pekin = new Town()
            {
                Name = "Pekin",
                Country = china
            };

            context.Towns.Add(buku);
            context.Towns.Add(pekin);
            context.SaveChanges();

        }
        public ActionResult Add(Country model)
        {
            if (ModelState.IsValid)
            {
                string strPath = HttpRuntime.BinDirectory + "\\Countries.xml";
                _provider = new CountryProvider(new CountryReader(strPath));

                List<Country> result = _provider.GetCountries().ToList();
                result.Add(model);

                if(_provider.SaveCountries(result))
                    return RedirectToAction("Index", "Country");
            }
            return View();
        }