Reputation: 11349
I need to get the current context given an entity
I found this old article from 2009
given that we're now firmly in 2012, is there another way ?
UPDATE - here are some details on where this is reqd
Table1 - at the heart of the star shaped schema Tags nvarchar(300) Idx identity int
Table2 - Tags reference TagID identity int TagText nvarchar(50)
Table3 - Rel between Table1 & Table2 IDX FK TagID FK
In the Context.SavingChanges() event for Table1 the tags are parsed and saved to Table2 with the relationships added to Table3 using context from Table1
Any guidance on how to improve this are welcome
Upvotes: 1
Views: 563
Reputation: 7523
If you are in the SavingChanges event handler, then you don't need to get the context from the entity, but rather the sender object of the event is the ObjectContext. This is the appropriate place to get the context in such a scenario. For example:
public void context_SavingChanges(object sender, EventArgs e)
{
ObjectContext context = sender as ObjectContext;
// Do whatever you need to do...
}
Upvotes: 1
Reputation:
The article shows how to get from IEntityWithRelationships, this is true, but if ud. For the current Context from the name of the entity, do not think you can do .... Maybe get the EntityContainerName:
public static string GetEntityContainerName(EntityObject entity)
{
string entityContainerName = entity.EntityKey.EntityContainerName;
return entityContainerName;
}
If you need reuse the current context, you need pass this as a parametter in your method.
regards
Upvotes: 0