AttachTo dosen't seem to work. Object is always null
I use entity Framework 4.
var entity = rep.GetEntityTxt<AreaTypeText>(PKey, LangID);
_db.AttachTo("AreaTypeText",entity);
entity.Txt = "TEST";
_db.SaveChanges();
When I check in debug mode on the _db variable, my AreaTypeText object is null. Nothing seem to be attached.
UPDATE WORKING EXAMPLE I need to detach to be able to change the EntityState to Modified before SaveChange.var pluralizer = PluralizationService.CreateService(CultureInfo.GetCultureInfo("en"));
TEntity entity = _db.CreateObjectSet<TEntity>().FirstOrDefault(p => p.ID == id && p.LangID == langid);
if (entity != null)
{
_db.Detach(entity);
entity.Txt = value;
_db.Ad开发者_运维知识库dObject(pluralizer.Pluralize(entity.GetType().Name), entity);
_db.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
_db.SaveChanges();
var pluralizer = PluralizationService.CreateService(CultureInfo.GetCultureInfo("en"));
TEntity entity = _db.CreateObjectSet<TEntity>().FirstOrDefault(p => p.ID == id && p.LangID == langid);
if (entity != null)
{
_db.Detach(entity);
entity.Txt = value;
_db.AddObject(pluralizer.Pluralize(entity.GetType().Name), entity);
_db.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
_db.SaveChanges();
精彩评论