Проблемы с обновлением w/ ef4 repo & mvc2-невозможно обновить график многих ко многим

StackOverflow https://stackoverflow.com/questions/4764370

Вопрос

Кажется, я не могу успешно обновить график для многих ко многим в MVC2 с помощью EF4. Я подумал, что самым простым, чтобы сделать, было бы очистить () весь график, вызовать savechanges (), а затем снова восстановить график Savechanges () в конце, но он не работает. Все другие мои свойства НАХОДЯТСЯ работа, однако. Во -первых, мои методы действия:

    public ActionResult EditReview(int id)
    {
        var game = _gameRepository.GetGame(id);
        var genres = _gameRepository.AllGenres();
        var platforms = _gameRepository.AllPlatforms();

        var model = new AdminGameViewModel { GameData = game, AllGenres = genres, AllPlatforms = platforms }; 

        return View(model);
    }

    //
    // POST: /Admin/EditReview/5

    [HttpPost]
    public ActionResult EditReview([Bind(Prefix="GameData")]Game existingGame, int[] PlatformIDs)
    {
        try
        {
            _gameRepository.ValidateGame(existingGame, PlatformIDs);
        }
        catch(RulesException ex)
        {
            ex.CopyTo(ModelState);
            ex.CopyTo(ModelState, "GameData");
        }

        if (ModelState.IsValid)
        {
            return RedirectToAction("Index");
        }
        else
        {
            var genres = _gameRepository.AllGenres();
            var platforms = _gameRepository.AllPlatforms();

            var model = new AdminGameViewModel { GameData = existingGame, AllGenres = genres, AllPlatforms = platforms };

            return View(model);
        }
    }

Сам репо (Validegame и SaveGame являются соответствующими методами):

namespace HandiGamer.Domain.Concrete
{
    public class HGGameRepository : IGameRepository
    {
        private HGEntities _siteDB = new HGEntities();

        public List<Game> Games
        {
            get { return _siteDB.Games.ToList(); }
        }

        public void ValidateGame(Game game, int[] PlatformIDs)
        {
            var errors = new RulesException<Game>();

            if (string.IsNullOrEmpty(game.GameTitle))
            {
                errors.ErrorFor(x => x.GameTitle, "A game must have a title");
            }

            if (string.IsNullOrEmpty(game.ReviewText))
            {
                errors.ErrorFor(x => x.ReviewText, "A review must be written");
            }

            if (game.ReviewScore <= 0 || game.ReviewScore > 5)
            {
                errors.ErrorFor(x => x.ReviewScore, "A game must have a review score, and the score must be between 1 and 5");
            }

            if (string.IsNullOrEmpty(game.Pros))
            {
                errors.ErrorFor(x => x.Pros, "Each game review must have a list of pros");
            }

            if (string.IsNullOrEmpty(game.Cons))
            {
                errors.ErrorFor(x => x.Cons, "Each game review must have a list of cons");
            }

            if (PlatformIDs == null || PlatformIDs.Length == 0)
            {
                errors.ErrorForModel("A game must belong to at least one platform");
            }

            if (game.GenreID == 0)
            {
                errors.ErrorFor(x => x.GenreID, "A game must be associated with a genre");
            }

            if (errors.Errors.Any())
            {
                throw errors;
            }
            else
            { 
                SaveGame(game, PlatformIDs);
            }
        }

        public void SaveGame(Game game, int[] PlatformIDs)
        {
            _siteDB.Games.Attach(game);

            if (game.GameID > 0)
            {
                _siteDB.ObjectStateManager.ChangeObjectState(game, System.Data.EntityState.Modified);

                game.Platforms.Clear();
            }
            else
            {
                _siteDB.ObjectStateManager.ChangeObjectState(game, System.Data.EntityState.Added);
            }

            foreach (int id in PlatformIDs)
            {
                Platform plat = _siteDB.Platforms.Single(pl => pl.PlatformID == id);
                game.Platforms.Add(plat);
            }

            game.LastModified = DateTime.Now;

            _siteDB.SaveChanges();
        }

        public Game GetGame(int id)
        {
            return _siteDB.Games.Include("Genre").Include("Platforms").SingleOrDefault(g => g.GameID == id);
        }

        public IEnumerable<Game> GetGame(string title)
        {
            return _siteDB.Games.Include("Genre").Include("Platforms").Where(g => g.GameTitle.StartsWith(title)).AsEnumerable<Game>();
        }

        public List<Game> GetGamesByGenre(int id)
        { 
            return _siteDB.Games.Where(g => g.GenreID == id).ToList();
        }

        public List<Game> GetGamesByGenre(string genre)
        {
            return _siteDB.Games.Where(g => g.Genre.Name == genre).ToList();
        }

        public List<Game> GetGamesByPlatform(int id)
        {
            return _siteDB.Games.Where(g => g.Platforms.Any(p => p.PlatformID == id)).ToList();
        }

        public List<Game> GetGamesByPlatform(string platform)
        {
            return _siteDB.Games.Where(g => g.Platforms.Any(p => p.Name == platform)).ToList();
        }

        public List<Genre> AllGenres()
        {
            return _siteDB.Genres.OrderBy(g => g.Name).ToList();
        }

        public List<Platform> AllPlatforms()
        { 
            return _siteDB.Platforms.OrderBy(p => p.PlatformID).ToList();
        }
    }
}

Я озадачен.

Это было полезно?

Решение

Кевин, О, это немного сложно и заставляет вас вернуться к узорам EFV1, потому что с M: M у вас нет иностранных ключей, на которые можно опираться, и вы застряли с наличием объектов.

Когда вы добавляете игру, вы хотите, чтобы отношения (то есть ряд в таблице соединения) добавлялись, но вы не хотите, чтобы платформа была добавлена, поскольку это просто ссылка.

Я на самом деле не сделал этого, но я думаю, что было бы проще, если бы вы могли разбить ее на части, а затем восстановить коллекцию платформ, как только игра будет прикреплена и помечена. В противном случае, если вы добавите весь график, все будет добавлено.

Проблема с EF заключается в том, что если вы прикрепите игру, вы также прикрепите связанные вещи. Там может быть более чистый рисунок, но я думаю, что отсоединить платформы от игры, прикрепить игру к контексту, отметьте ее в виде добавления. Тогда я бы прикрепил платформы к контексту. Они будут "без изменений". Затем добавьте их в коллекцию Games.Platform. Платформы все еще останутся неизменными, но : будет понято.

Вы, возможно, попробовали это. Я должен был сделать это сам и наблюдать за тем, как все, что я иду, наверняка убедиться, что происходит. Ключ в том, что EF должен отслеживать, что отношение был добавлен и это новое (и приведет к тому, что в добавленной таблице соединений), но понимает, что платформы не являются новыми.

HTH Джули

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top