Change track not working with Entity framework core

entity-framework-core postgresql

Question

I want to track the changes to entities that I update using Entity framework core. The issue is that it always gives me no changes but database is updated. I am using PostgreSQL database with entity framework core.Below is my configurations:

Context.cs:

    public class TestContext : DbContext
    {
        public QRDAParsingContext(DbContextOptions<QRDAParsingContext> options) : base(options)
        {
            //options.UseSqlServer(ConfigurationManager.ConnectionStrings["BloggingDatabase"].ConnectionString)
        }
        public DbSet<Individual> Individual { get; set; }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
        }

        public override int SaveChanges()
        {
            return base.SaveChanges();
        }
    }

Startup.cs:

        public void ConfigureServices(IServiceCollection services)
        {
            JwtTokenDefinitions.LoadFromConfiguration(Configuration);
            services.ConfigureJwtAuthentication();
            services.AddMvc();
            services.AddCors();
            services.AddEntityFrameworkNpgsql().AddDbContext<QRDAParsingContext>(options => options.UseNpgsql(Configuration.GetConnectionString("DataAccessPostgreSqlProvider")));

            services.AddMvc().AddJsonOptions(options =>
            {
                options.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
                options.SerializerSettings.ContractResolver =
                   new Newtonsoft.Json.Serialization.DefaultContractResolver();
            });
            // Register the Swagger generator, defining one or more Swagger documents

            var corsBuilder = new CorsPolicyBuilder();
            corsBuilder.AllowAnyHeader();
            corsBuilder.AllowAnyMethod();
            corsBuilder.AllowAnyOrigin(); // For anyone access.
            corsBuilder.AllowCredentials();
            services.AddCors(options =>
            {
                options.AddPolicy("SiteCorsPolicy", corsBuilder.Build());
            });

        }

Code that gives issue:

 context.Entry(updateIndividualNew).State = EntityState.Modified;
 context.SaveChanges();
 var changes = context.ChangeTracker.Entries().Where(p => p.State == EntityState.Modified).ToList();
1
0
2/23/2018 7:23:16 AM

Popular Answer

Hi i have example how to tracking entity changes and save to db infromation about changes https://github.com/siasty/EntityChangeTracking.git

context.Entry(updateIndividualNew).State = EntityState.Modified;
DisplayTrackedEntities(context.ChangeTracker);
context.SaveChanges();


 private static void DisplayTrackedEntities(ChangeTracker changeTracker)
 {

            var entries = changeTracker.Entries();
            foreach (var entry in entries)
            {
                Console.WriteLine("Table Name: {0}", entry.Entity.GetType().Name);
                Console.WriteLine("Status: {0}", entry.State);
                foreach (var prop in entry.OriginalValues.Properties)
                {
                    var originalValue = entry.OriginalValues[prop].ToString();
                    var currentValue =  entry.CurrentValues[prop].ToString();
                    if (originalValue != currentValue)
                    {
                        Console.WriteLine("Orginal: Column Name {0} value {1} ", prop.Name, entry.OriginalValues[prop].ToString());
                        Console.WriteLine("Current: Column Name {0} value {1}", prop.Name, entry.CurrentValues[prop].ToString());
                    }
                }
            }

}
1
9/5/2018 12:54:30 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow