Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if a given Linq-to-Sql object is attached to a datacontext?

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.

like image 471
Thenon Avatar asked May 08 '09 17:05

Thenon


1 Answers

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
like image 108
David Avatar answered Sep 22 '22 19:09

David