Using code-first, I had doubts over what would happen if I obtained an object by querying the database, but kept using it after having it deleted or removed from the database.
For example, I performed this test:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication12
{
class Program
{
static void Main(string[] args)
{
using (TestDB db = new TestDB())
{
Usuario myUser = db.Users.FirstOrDefault();
if (myUser != null)
{
Console.WriteLine(myUser.Name);
db.Users.Remove(myUser);
db.SaveChanges();
Console.WriteLine(myUser.Name);
}
}
}
}
}
The test worked and the same myUser.Name was printed to the console twice. With myUser by default being a tracked object, I expected the program to crash. However it seems that the objects with all its values persisted after it was removed. Did myUser essentially become a local object and stop being tracked by the context? Would it cause any problems should I return this deleted entity through a function and store it simply as a class attribute?
myUseris a worker that just got fired, but retains his skillsets (a.k.a attributes)? :)