I came across what, in my opinion, is a very strange situation with the infrastructure of the entity. Basically, if I update a row directly with the sql command, when I return this row via linq, it has no updated information. See the example below for more information.
First I created a simple DB table
CREATE TABLE dbo.Foo ( Id int NOT NULL PRIMARY KEY IDENTITY(1,1), Name varchar(50) NULL )
Then I created a console application to add an object to the database, update it using the sql command, and then restore the newly created object. There he is:
public class FooContext : DbContext { public FooContext() : base("FooConnectionString") { } public IDbSet<Foo> Foo { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Foo>().ToTable("Foo"); base.OnModelCreating(modelBuilder); } } public class Foo { [Key] public int Id { get; set; } public string Name { get; set; } } public class Program { static void Main(string[] args) {
The line below writes "Before," however I expect it to display "After." The strange thing is that if I run the profiler, I see that the sql query is being executed, and if I run the query in the management studio, it returns "After" as the name. I am starting sql server 2014.
Can someone please help me understand what is going on here?
UPDATE:
It will go to the database by the line FirstOrDefault. Please see the attached screenshot from the sql profiler.

So my question is actually this:
1) If this is caching, should it not be in the database? Is this a bug in EF?
2) If it goes to db and spends resources, EF should not update object.