Quantcast
Channel: Entity Framework
Viewing all articles
Browse latest Browse all 10318

New Post: Entity's state isn't updated

$
0
0

Well, I began playing around with Code First.

I have created simple database and appropriate code for Code First (VB 11).

Class FLContext : Inherits DbContext

    Property Districts As DbSet(Of District)
    Property Producers As DbSet(Of Producer)

    SubNew()
    EndSubSubNew(nameOrConnectionString AsString)
        MyBase.New(nameOrConnectionString)
    EndSubProtectedOverridesSub OnModelCreating(modelBuilder As DbModelBuilder)
        With modelBuilder.Configurations
            .Add(New DistrictConfiguration)
            .Add(New ProducerConfiguration)
        EndWithEndSubEndClassClass DistrictConfiguration : Inherits EntityTypeConfiguration(Of District)
    SubNew()
        ToTable("District")
        HasKey(Function(p) p.Id)
        [Property](Function(p) p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None)
    EndSubEndClassClass ProducerConfiguration : Inherits EntityTypeConfiguration(Of Producer)
    SubNew()
        ToTable("Producer")
        HasKey(Function(p) p.Id)
        [Property](Function(p) p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None)
    EndSubEndClassClass District
    OverridableProperty Id AsIntegerOverridableProperty Num AsIntegerEndClassClass Producer
    OverridableProperty Id AsIntegerOverridableProperty Num AsIntegerEndClass

Then I created console application to test this out (SqlServer class just retrieves connection string):

Sub TestCodeFirst()

    Dim server = New SqlServer(ServerEnum.Local)
    db = New FLContext(server.ConnectionString)
    db.Database.Initialize(True)

    Console.WriteLine("Properties:")
    Console.WriteLine("AutoDetectChangesEnabled: {0}", db.Configuration.AutoDetectChangesEnabled)
    Console.WriteLine("LazyLoadingEnabled: {0}", db.Configuration.LazyLoadingEnabled)
    Console.WriteLine("ProxyCreationEnabled: {0}", db.Configuration.ProxyCreationEnabled)
    Console.WriteLine("ValidateOnSaveEnabled: {0}", db.Configuration.ValidateOnSaveEnabled)
    Console.WriteLine()

    ' Delete any existing data
    db.Database.ExecuteSqlCommand("delete from dbo.District;", {})
    db.Database.ExecuteSqlCommand("delete from dbo.Producer;", {})

    Console.WriteLine("Adding districts...")
    With db.Districts
        .Add(New District With {.Id = 1, .Num = 11})
        .Add(New District With {.Id = 2, .Num = 44})
    EndWith

    db.SaveChanges()

    Console.WriteLine("Changing Num property for District with Id = 2...")
    Dim dist = db.Districts.Where(Function(d) d.Id = 2).First()
    Dim entry = db.Entry(dist)
    Console.WriteLine("The state when retrieved: {0}", entry.State)
    ' Change "Num" property
    dist.Num = 22
    'db.ChangeTracker.DetectChanges()
    Console.WriteLine("The state when changed: {0}", entry.State) 'Should be "Modified"
    db.SaveChanges()

EndSub

Here's the output:

What is strange is that the entity's state after modifying remains "Unchanged". As you see, "ProxyCreationEnabled" is set to True, hence modifying property should change the state of entity, because all properties of my classes are Overridable. But it didn't happen.

In this case I can:

  1. Uncomment db.ChangeTracker.DetectChanges()" line.
  2. Force to trigger DetectChanges with db.Entry(dist).State

However, this defeats the purpose of having proxy classes. This happens in both EF5 and EF6.


Viewing all articles
Browse latest Browse all 10318

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>