1
EF4でコンテンツのリストを更新しようとしています。しかし、私はそれを行う方法を発見していない。次のように私の現在のコードは次のとおりです。リストを更新する<Entity>(EF4内)
db.NotifContents.MergeOption = MergeOption.NoTracking;
List<NotifContent> notifContentList = db.NotifContents
.Where(u => u.FKID_Contact == contactId && !u.Sent && u.CanSendMail)
.ToList();
List<NotifContent> newNotifContentList = new List<NotifContent>();
foreach(NotifContent notifContent in notifContentList)
{
notifContent.NextMailSend = dtNextMailSend; newNotifContentList.Add(notifContent);
try
{
//db.CreateObjectSet<NotifContent>().Attach(notifContent);
//db.ObjectStateManager.ChangeObjectState(notifContent, System.Data.EntityState.Modified);
db.AddToNotifContents(notifContent);
ObjectStateEntry notifContentsEntry = db.ObjectStateManager
.GetObjectStateEntry(notifContent);
notifContentsEntry.ChangeState(EntityState.Modified);
db.SaveChanges();
}
catch (Exception exc)
{
}
}
エンティティをDataContextのに添付されているので2回目の更新は常に当然失敗し、そしてだろうが。
EF4で一括更新を行うにはどうすればよいですか?
UPDATE
変更ループ
foreach(NotifContent notifContent in notifContentList)
{
notifContent.NextMailSend = dtNextMailSend;
db.NotifContents.Attach(notifContent);
db.ObjectStateManager.ChangeObjectState(notifContent, EntityState.Modified);
}
db.SaveChanges();
と、私は次のようなエラーになっています次のように: - すでにObjectStateManagerに存在する同じキーで
オブジェクトを。 ObjectStateManagerは、同じキーを持つ複数のオブジェクトを追跡できません。
db.AddToNotifContents'を行う '何が? –
エンティティをセット番号に追加しますか? – Johann
申し訳ありませんが、もちろん、それはオブジェクトコンテキストで生成されたメソッドです。 –