views:

79

answers:

1

Why am I getting a exception when ApplyPropertyChanges???

The code is almost the same when I'm editing a user table but is not working with my news table.

The create, delete and details are all working fine but when I try to edit a news I'm getting the exception below:

The ObjectStateManager does not contain a ObjectStateEntry 'MagixCMS.Models.noticia'

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MagixCMS.Models
{
    public class NoticiaRepository : INoticiaRepository
    {
        #region INoticiaRepository Members

        magixcmsEntities _entities = new magixcmsEntities();

        public noticia CreateNoticia(noticia noticiaToCreate)
        {
            _entities.AddTonoticiaSet(noticiaToCreate);
            _entities.SaveChanges();
            return noticiaToCreate;
        }

        public void DeletaNoticia(noticia noticiaToDelete)
        {
            var noticiaOriginal = GetNoticia(noticiaToDelete.Id);
            _entities.DeleteObject(noticiaOriginal);
            _entities.SaveChanges();
        }

        public noticia EditNoticia(noticia noticiaToEdit)
        {
            var noticiaOriginal = GetNoticia(noticiaToEdit.Id);
            _entities.ApplyPropertyChanges(noticiaToEdit.EntityKey.EntitySetName, noticiaToEdit); //EXCEPTION HERE
            _entities.SaveChanges();
            return noticiaToEdit;
        }

        public noticia GetNoticia(int id)
        {
            return (from c in _entities.noticiaSet where c.Id == id select c).FirstOrDefault();
        }

        public IEnumerable<noticia> ListNoticias()
        {
            return _entities.noticiaSet.ToList();
        }

        #endregion
    }
}

I google the exception and didn't found much help.

A: 

I solve it.

The problem is on the EF model.

To solve it you'll need a extension method to persist your data:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Objects;
using System.Data.Objects.DataClasses;

namespace MagixCMS.Models
{
    public static class Extensions
    {
        public static void Update(ObjectContext context, string entitySetName, IEntityWithKey entity)
        {
            entity.EntityKey = context.CreateEntityKey(entitySetName, entity);
            context.Attach(entity);
            var stateEntry = context.ObjectStateManager.GetObjectStateEntry(entity.EntityKey);
            var propertyNameList = stateEntry.CurrentValues.DataRecordInfo.FieldMetadata.Select(pn => pn.FieldType.Name);
            foreach (var propName in propertyNameList)
            {
                stateEntry.SetModifiedProperty(propName);
            }
        }
    }
}

And in the Edit method you do:

public noticia EditNoticia(noticia noticiaToEdit)
{
    //GET THE CONTEXT FOR THE ENTITY
    ObjectContext _context = this._entities.noticiaSet.Context;
    var noticiaOriginal = GetNoticia(noticiaToEdit.Id);
    //UPDATE THE ORIGINAL ENTITY WITH THE NEW VALUES
    Extensions.Update(_context, noticiaOriginal.EntityKey.EntitySetName, noticiaToEdit);
    //PERSIST THE DATA
    _entities.SaveChanges();
    return noticiaToEdit;
}
John Prado