开发者

AttachTo dosen't seem to work. Object is always null

开发者 https://www.devze.com 2023-01-24 06:26 出处:网络
I use entity Framework 4. var entity = rep.GetEntityTxt<AreaTypeText>(PKey, LangID); _db.AttachTo(\"AreaTypeText\",entity);

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

精彩评论

暂无评论...
验证码 换一张
取 消