更改跟踪不与Entity框架核心一起使用的跟踪

时间:2018-02-23 07:18:02

标签: postgresql entity-framework-core

我想跟踪使用Entity framework core更新的实体的更改。问题是它始终没有给我更改,但数据库已更新。我正在使用PostgreSQL数据库和实体框架core.Below是我的配置:

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());
            });

        }

提出问题的代码:

 context.Entry(updateIndividualNew).State = EntityState.Modified;
 context.SaveChanges();
 var changes = context.ChangeTracker.Entries().Where(p => p.State == EntityState.Modified).ToList();

1 个答案:

答案 0 :(得分:0)

嗨,我有一个示例,该示例如何跟踪实体更改并将有关更改的信息保存到数据库 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());
                    }
                }
            }

}