コード例 #1
0
        public int GetId(string title)
        {
            Type element = context.Types.FirstOrDefault(rec => rec.Title == title);

            if (element != null)
            {
                return(element.Id);
            }
            throw new Exception("Форма не найдена");
        }
コード例 #2
0
        public void AddElement(TypeModel model)
        {
            Type element = context.Types.FirstOrDefault(rec => rec.Title == model.Title);

            if (element != null)
            {
                throw new Exception("Уже есть такая форма");
            }
            context.Types.Add(new Type
            {
                Title = model.Title
            });
            context.SaveChanges();
        }
コード例 #3
0
        public TypeModel GetElement(int id)
        {
            Type element = context.Types.FirstOrDefault(rec => rec.Id == id);

            if (element != null)
            {
                return(new TypeModel
                {
                    Id = element.Id,
                    Title = element.Title
                });
            }
            throw new Exception("Элемент не найден");
        }
コード例 #4
0
        public void DelElement(int id)
        {
            Type element = context.Types.FirstOrDefault(rec => rec.Id == id);

            if (element != null)
            {
                context.BookTypes.RemoveRange(context.BookTypes.Where(rec => rec.TypeId == id));
                context.Types.Remove(element);
                context.SaveChanges();
            }
            else
            {
                throw new Exception("Элемент не найден");
            }
        }
コード例 #5
0
        public void UpdElement(TypeModel model)
        {
            Type element = context.Types.FirstOrDefault(rec => rec.Title ==
                                                        model.Title && rec.Id != model.Id);

            if (element != null)
            {
                throw new Exception("Уже есть такая форма");
            }
            element = context.Types.FirstOrDefault(rec => rec.Id == model.Id);
            if (element == null)
            {
                throw new Exception("Элемент не найден");
            }
            element.Title = model.Title;
            context.SaveChanges();
        }