Exemple #1
0
        public IHttpActionResult PutPerson(int id, Person person)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != person.PersonID)
            {
                return(BadRequest());
            }

            db.Entry(person).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PersonExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public ActionResult Create([Bind(Include = "PersonId,LastName,FirstName")] Person person)
        {
            if (ModelState.IsValid)
            {
                db.Persons.Add(person);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(person));
        }
        public ActionResult Create([Bind(Include = "IdPerson,name,lastname,BDate,Photo,Describe,Gender,LostDate,LostPlace")] LostPerson lostPerson)
        {
            if (ModelState.IsValid)
            {
                db.LostPerson.Add(lostPerson);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.Gender = new SelectList(db.Gender, "Id", "Name", lostPerson.Gender);
            return(View(lostPerson));
        }
        public void TestMethod1()
        {
            using (var context = new PersonEntities())
            {
                var person = context.People.Add(
                    new Person()
                {
                    FirstName = "Mary", LastName = "Moore"
                });

                person.Phones = new List <Phone>()
                {
                    new Phone()
                    {
                        PhoneType = "Cell 1", PhoneNumber = "888-888-8888"
                    },
                    new Phone()
                    {
                        PhoneType = "Cell 2", PhoneNumber = "666-666-6666"
                    },
                    new Phone()
                    {
                        PhoneType = "Cell 3", PhoneNumber = "444-444-444"
                    }
                };

                context.SaveChanges();
            }
        }
Exemple #5
0
        /// <summary>
        /// Removes test data from database, called from all tests that create data
        /// </summary>
        /// <remarks>
        /// If you have issues with data not disposing then set break-points
        /// in the emppty try/catch statements to figure out the issue. More likely
        /// than not the interface, in this case IBaseEntity was not implemented on
        /// one of the classes.
        ///
        /// The try-catches allow us to continue and throw an exception message in
        /// the tear down event TeardownTestBase for any test.
        ///
        /// Empty try/catches are okay here as you should be using this only for
        /// unit testing and hopefully on a non production database.
        ///
        /// </remarks>
        public bool AnnihilateData(List <object> mAnnihilateList)
        {
            bool mAnnihilateDataSuccessful = false;

            using (var destroyContext = new PersonEntities())
            {
                for (int i = mAnnihilateList.Count - 1; i >= 0; i--)
                {
                    try
                    {
                        var currentObject = mAnnihilateList[i];

                        var existingItem = destroyContext
                                           .Set(currentObject.GetType())
                                           .Find(((IBaseEntity)currentObject).Identifier);

                        if (existingItem != null)
                        {
                            try
                            {
                                var attachedEntry = destroyContext.Entry(existingItem);
                                attachedEntry.CurrentValues.SetValues(currentObject);
                                destroyContext.Set(existingItem.GetType()).Attach(existingItem);

                                destroyContext.Set(existingItem.GetType()).Remove(existingItem);
                            }
                            catch (Exception)
                            {
                                // ignore nothing do to as the object was not added in properly.
                            }
                        }
                        else
                        {
                            var item = currentObject.GetType();
                        }
                    }
                    catch (Exception)
                    {
                        //catch and continue save what we can
                    }
                }
                try
                {
                    var resultCount      = destroyContext.SaveChanges();
                    var annihlationCount = mAnnihilateList.Count;

                    mAnnihilateDataSuccessful = (resultCount == annihlationCount);
                }
                catch (Exception)
                {
                    // keep on going
                }
                finally
                {
                    destroyContext.Dispose();
                }
            }

            return(mAnnihilateDataSuccessful);
        }
Exemple #6
0
 public void Delete(decimal id)
 {
     using (var context = new PersonEntities())
     {
         var record = (from d in context.Person select d).Where(d => d.Id.Equals(id)).FirstOrDefault();
         context.Person.Remove(record);
         context.SaveChanges();
     }
 }
Exemple #7
0
 public void Update(PersonModel p)
 {
     using (var context = new PersonEntities())
     {
         var query = (from d in context.Person select d).Where(d => d.Id.Equals(p.Id)).FirstOrDefault();
         query.Name   = p.Name;
         query.Gender = p.Gender;
         query.Phone  = p.Phone;
         context.SaveChanges();
     }
 }
        public void TestInitialize()
        {
            using (var entities = new PersonEntities())
            {
                entities.People.RemoveRange(entities.People);
                entities.SaveChanges();

                var tokens = LoremIpsum.Split(' ');

                foreach (var token in tokens)
                {
                    entities.People.Add(new Person
                    {
                        Name     = token,
                        PersonId = Guid.NewGuid()
                    });
                }

                entities.SaveChanges();
            }
        }
Exemple #9
0
        public void Create(PersonModel p)
        {
            using (var context = new PersonEntities())
            {
                Person pe = new Person();
                pe.Name   = p.Name;
                pe.Gender = p.Gender;
                pe.Phone  = p.Phone;

                context.Person.Add(pe);
                context.SaveChanges();
            }
        }
Exemple #10
0
        public Form1()
        {
            InitializeComponent();

            PersonEntities ente        = new PersonEntities();
            master_file    master_file = (new master_file()
            {
                firstname = "My", lastname = "Lastname", function = EFunction.Interessent, aktiv = true, deleted_inaktiv = false, newsletter_flag = true
            });

            ente.master_file.Add(master_file);
            ente.SaveChanges();
        }
Exemple #11
0
        public string DeleteById(int id)
        {
            ///delete the Relations from Documents to Classes
            List <DocumentClass> relationList = entities.document_class.Where(x => x.doc_id == id).ToList();

            foreach (var item in relationList)
            {
                entities.document_class.Remove(item);
            }
            /////set the affected Absences - DocumentId to null
            //List<Absence> absenceList = entities.Absences.Where(x => x.DocumentId == id).ToList();
            //foreach (var item in absenceList)
            //{
            //    item.DocumentId = null;
            //    entities.Absences.Update(item);
            //}
            ///set the affected Communications - DocumentId to null
            List <Communication> communicationList = entities.communication.Where(x => x.DocumentId == id).ToList();

            foreach (var item in communicationList)
            {
                item.DocumentId = null;
                entities.communication.Update(item);
            }

            Document documentToDelete = entities.documents.SingleOrDefault(x => x.Id == id);

            if (documentToDelete == null)
            {
                return("The Document you want to delete could not be found.");
            }
            ///Deletes Document with its Path
            bool fileFound = DeleteRealDocument(documentToDelete);

            ///Deletes Document entry in Database
            entities.documents.Remove(documentToDelete);
            entities.SaveChanges();

            if (fileFound)
            {
                return("Record has been successfully deleted");
            }
            else
            {
                return("File not found.");
            }
        }
Exemple #12
0
 public virtual void Create(T toAdd)
 {
     entities.Set <T>().Add(toAdd);
     entities.SaveChanges();
 }