I'm using Linq to Entities to perform database operations .
Problem is if data is not there in cache object I tried to take it from database using single thread.
it gives me following error :
The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe.
to overcome I tried the following solution:
using (var tran = new TransactionScope())
{
Task.Factory.StartNew(() => DoSomething());
Task.Factory.StartNew(() => DoSomethingElse());
// Wait all
tran.Complete();
}
but instead if this can I do it using another thread running:
//My code
//thread to run Method1()
method 1()
{
//DO SOMETHING
//CALL METHOD2()
}
Is it good to simply do like:
string res = null;
Thread newThread = new Thread(() => {res = Method2();});