コード例 #1
0
        public string SaveItem(string footBaller)
        {
            string result = "";

            try
            {
                Footballers footballers = JsonConvert.DeserializeObject <Footballers>(footBaller);
                if (footballers.Id == 0)
                {
                    FC.Add(footballers);
                    FC.SaveChanges();
                    result = JsonConvert.SerializeObject(new { status = "OK" });
                }
                else
                {
                    FC.Entry(footballers).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                    FC.SaveChanges();
                    result = JsonConvert.SerializeObject(new { status = "OK" });
                }
            }
            catch (Exception ex)
            {
                result = JsonConvert.SerializeObject(new { status = "Error", Error = ex.ToString() });
            }
            return(result);
        }
コード例 #2
0
        public static void Initialize(FootballersContext context)
        {
            if (!context.Footballers.Any())
            {
                context.Footballers.Add(new Models.Footballers
                {
                    FirstName = "Эдуард",
                    LastName  = "Стрельцов",
                    Gender    = "Мужской",
                    BirthDate = Convert.ToDateTime("21.07.1937").Date,
                    NameTeam  = "Торпедо",
                    Country   = "Россия"
                });

                context.Footballers.Add(new Models.Footballers
                {
                    FirstName = "Эбби",
                    LastName  = "Уомбак",
                    Gender    = "Женский",
                    BirthDate = Convert.ToDateTime("02.06.1980").Date,
                    NameTeam  = "Washington Freedom",
                    Country   = "США"
                });

                context.SaveChanges();
            }
        }