Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Property is not tracked by EF Core

$
0
0

I have a very mysterious issue with my Blazor application. It's a big form, the main object has around 20 primitive properties and 10 complex ones. Almost everything works as expected, but there is one property that somehow does not get tracked by EF.

For simplicity, consider the following code:

public class Class1 {    public int? Property1 {get;set;}    public int? Property2 {get;set;}}// Contextprotected override void OnModelCreating(ModelBuilder modelBuilder) {    modelBuilder.Entity<Class1>(entity =>     {        entity.Property(e => Property1).HasDefaultValue(1);        entity.Property(e => Property2).HasDefaultValue(1);    });}// --// lets assume the entry in the DB already exists and Property2 has value 0.var InstanceOfClass1 = await GetInstanceOfClassFromDB();InstanceOfClass1.Property1++;InstanceOfClass1.Property2++;Console.WriteLine(InstanceOfClass1.Property1); // 1Console.WriteLine(InstanceOfClass1.Property2); // 1using var context = await _Factory.CreateDbContextFactoryAsync();context.Classes.Update(InstanceOfClass1);await context.SaveChangesAsync();Console.WriteLine(InstanceOfClass1.Property1); // 1Console.WriteLine(InstanceOfClass1.Property2); // 0

This is what I'm experiencing.

If I add context.Entry(InstanceOfClass1).Property(e => e.Property2).IsModified = true; before context.Classes.Update(InstanceOfClass1), it works as expected. But I dont understand this. I have never seen this behaviour before, there is no other property that behaves like this. Property2 is a FK on a different table, just like other props of that object. There is no exception, no warning, nothing.

I've tried to remove the default value in the DB and reverse engineered the context again, doesn't help.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



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