Esempio n. 1
0
        public IHttpActionResult Create(Class @class)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            // avoiding duplicate or changed entities
            foreach (var entry in @class.AvailableRaces)
            {
                entry.Faction = null; // else ef tries to add the faction again
                Context.Entry(entry).State = EntityState.Unchanged;
            }

            Context.Classes.Add(@class);
            Context.SaveChanges();

            return Ok();
        }
Esempio n. 2
0
        public IHttpActionResult Update(int id, Class @class)
        {
            @class.Id = id;

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            // really f*****g dirty, but working!!!

            var entry = Context.Entry(@class);
            var tmp = new Race[@class.AvailableRaces.Count];
            @class.AvailableRaces.CopyTo(tmp, 0);

            @class.AvailableRaces.Clear();
            entry.State = EntityState.Modified;

            entry.Collection(i => i.AvailableRaces).Load();
            @class.AvailableRaces.Clear();

            foreach (var race in tmp)
            {
                @class.AvailableRaces.Add(Context.Races.Find(race.Id));
            }

            Context.SaveChanges();

            return Ok();
        }