时间:2010-11-11 03:15:44

标签: asp.net-mvc-2 entity-framework-4 repository-pattern

我使用实体框架4。

 var entity = rep.GetEntityTxt<AreaTypeText>(PKey, LangID);

 _db.AttachTo("AreaTypeText",entity);
 entity.Txt = "TEST";
 _db.SaveChanges();

当我在_db变量上检查调试模式时,我的AreaTypeText对象为null。似乎没有附加任何东西。


更新工作示例
我需要分离才能在SaveChange之前将EntityState更改为Modified。

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();

1 个答案:

答案 0 :(得分:1)

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();