0

I've learned that entities are classes that represent the tables inside a database schema. But on this link they speak of an entity being tracked (attached) by the context

http://msdn.microsoft.com/en-us/data/jj592676.aspx

Added: the entity is being tracked by the context...

Do they speak of the objects/instances of entity's (so the classes) getting tracked by Entity Framework? Or literally the entity itself? I'm confused.

In one of my webapplications I am using this code, does it say all instances of FinalStudyDecision are in the modified state, or just the object fsd?

context.FinalStudyDecisions.Attach(fsd);
context.Entry(fsd).State = EntityState.Modified;

Or does this code just do it for one single object?

ObjectStateManager.ChangeObjectState(fsd, EntityState.Modified);
4

1 に答える 1

1

エンティティがコンテキストに関連付けられると、コンテキストはオブジェクトを「認識」し、その変更の追跡を開始します。

通常、エンティティは、データベースからフェッチされるときにコンテキストに関連付けられますcontext.FinalStudyDecisions.Single(x => x.Id == 1)(もちろん、ID == 1 のアイテムがある場合)。

FinalStudyDecisionsただし、コンテキストによって認識されていない既存のものがある場合は、Attachメソッドを使用してそれを認識させることができます。これは通常、オブジェクトが Web クライアントにシリアライズされ、その後 Web クライアントからデシリアライズされるときに発生します。

オブジェクトをコンテキストにアタッチすると、その状態 ( EntityState) がになるため、Web クライアントからオブジェクトを受け取ったときにUnchangedその状態を変更することがよくあります。これにより、が呼び出されたModifiedときに EF がオブジェクトを格納するようになります。SaveChanges

両方

// DbContext API
context.Entry(fsd).State = EntityState.Modified;

// ObjectContext API
ObjectStateManager.ChangeObjectState(fsd, EntityState.Modified);

オブジェクトの状態のみを変更しfsdます。

重要:状態を設定するか(または) にオブジェクトを追加することAddによってオブジェクトをコンテキストに追加すると、そのすべての子オブジェクトだけでなく、すべての子オブジェクトが に変更されます。AddedDbSetObjectSetfsdAdded

于 2013-06-02T19:31:08.037 に答える