How can I tell if a given Linq-to-Sql object is attached to a datacontext?
-
20-08-2019 - |
Question
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.
Solution
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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow