示例#1
0
        /// <summary>
        /// Add method, adds a new mecanic
        /// </summary>
        /// <param name="nume"></param>
        /// <param name="prenume"></param>
        public static void Add(string nume, string prenume)
        {
            using (ModelCarServiceContainer context = new ModelCarServiceContainer())
            {
                context.Database.Connection.Open();

                try
                {
                    Mecanic mecanic = new Mecanic()
                    {
                        Nume    = nume,
                        Prenume = prenume
                    };
                    context.MecanicSet.Add(mecanic);
                    context.SaveChanges();
                }
                catch (DbEntityValidationException ex)
                {
                    foreach (DbEntityValidationResult entityErr in ex.EntityValidationErrors)
                    {
                        foreach (DbValidationError error in entityErr.ValidationErrors)
                        {
                            throw;
                        }
                    }
                }
            }
        }
示例#2
0
        /// <summary>
        /// Delete method, deletes an object from the DB. It requires the id of the object
        /// </summary>
        /// <param name="id"></param>
        public static void Delete(int id)
        {
            using (ModelCarServiceContainer context = new ModelCarServiceContainer())
            {
                context.Database.Connection.Open();

                Mecanic mecanic = new Mecanic()
                {
                    Id = id
                };

                context.MecanicSet.Attach(mecanic);
                context.MecanicSet.Remove(mecanic);
                context.SaveChanges();
            }
        }
示例#3
0
        /// <summary>
        /// Return an object of type Mecanic or null if the object is not found
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Mecanic GetById(int id)
        {
            using (ModelCarServiceContainer context = new ModelCarServiceContainer())
            {
                context.Database.Connection.Open();


                var _mecanic = context.MecanicSet.SingleOrDefault(m => m.Id == id);
                if (_mecanic != null)
                {
                    Mecanic mecanic = new Mecanic()
                    {
                        Id = id
                    };
                    context.MecanicSet.Attach(mecanic);
                    return(mecanic);
                }
            }
            return(null);
        }
示例#4
0
        /// <summary>
        /// Update method. Requires <paramref name="id"/> of the entity Mecanic and
        /// the new values for <paramref name="nume"/> and <paramref name="prenume"/> to be changed
        /// </summary>
        /// <param name="id"></param>
        /// <param name="nume"></param>
        /// <param name="prenume"></param>
        public static void Update(int id, string nume, string prenume)
        {
            Mecanic mecanic = new Mecanic()
            {
                Id      = id,
                Nume    = nume,
                Prenume = prenume
            };

            using (ModelCarServiceContainer context = new ModelCarServiceContainer())
            {
                context.Database.Connection.Open();
                var result = context.MecanicSet.SingleOrDefault(m => m.Id == id);
                if (result != null)
                {
                    result.Nume    = nume;
                    result.Prenume = prenume;
                    context.SaveChanges();
                }
            }
        }