views:

253

answers:

2

I use linq 2 sql for my ORM.

For performance reasons, I serialize some of them, and throw them into memcached.

When they're deserialized, they're of course not attached to a datacontext, which is 100% fine, as they're only used for reading from in those scenarios.

For sanity reasons however, I'd like to be able to tell whether a given object is attached to a datacontext (fetched from the db), or not (fetched from memcached).

Any ideas?

Thanks.

+1  A: 

Use GetOriginalEntityState. Here's a test.

Customer cust = new Customer();
ctx.Customers.Attach(cust);

Customer orig = ctx.Customers.GetOriginalEntityState(cust);

//test if orig is null
David Liddle
Thanks David, that does seem to work. However, when you try to attach an entity that did come from the context, it throws an exception, which could get expensive if this check was run frequently as a guard :\
Thenon
A: 

Hi Thenon,

I am trying to serialize linq 2 sql object and throw them to memcached without any luck. I try to serialize them by setting the serialization mode to unidirectional, but that didn't work.

could you please tell me how you manage to do that? thank you very much.

Maxi
Have a look at binary serialization of SerializableEntity here: http://www.codeproject.com/KB/linq/linqsqlserialization.aspx?display=Print
Thenon