Пример #1
0
        public static void Validate(Teammate teammate)
        {
            ModelValidator.Validate(teammate as object);
            var children = teammate.GetType().GetProperties().Where(x => x.GetValue(teammate) is IEntity);

            children.ToList().ForEach(x => ModelValidator.Validate(x));
        }
        public Teammate InsertOrUpdateDetached(Teammate entity)
        {
            var attachedTeammate = _context.Teammate.FirstOrDefault(x => x.Id == entity.Id);

            if (attachedTeammate == null)
            {
                attachedTeammate = _context.Teammate.Create();
                if (entity.GetType().Equals(attachedTeammate.GetType()))
                {
                    _context.Entry(entity).State = System.Data.Entity.EntityState.Modified;
                    return(entity);
                }
                else
                {
                    attachedTeammate = _context.Teammate.Add(attachedTeammate);
                }
            }
            var attachedEntry = _context.Entry(attachedTeammate);

            attachedEntry.CurrentValues.SetValues(entity);
            return(attachedTeammate);
        }