question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I have install this nuget packet in my sample project. But this don’t create any historie data in my database. Here my implementation:

DbContext:

public class BlogDataDbContext: DbContext
{
        public BlogDataDbContext(DbContextOptions<BlogDataDbContext> options): base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.EnableAutoHistory(null);
        }

        

        public DbSet<Blog> Blogs { get; set; }
}

Somemethod of saving:

 public class EditModel : PageModel
    {
        private readonly BlogDataDbContext _context;

        public EditModel(BlogDataDbContext context)
        {
            _context = context;
            _context.EnsureAutoHistory();
        }

        [BindProperty]
        public Data.BlogData.Blog Blog { get; set; }

        public async Task<IActionResult> OnGetAsync(Guid? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            Blog = await _context.Blogs.SingleOrDefaultAsync(m => m.BlogId == id);

            if (Blog == null)
            {
                return NotFound();
            }
            return Page();
        }

        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            _context.Attach(Blog).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BlogExists(Blog.BlogId))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return RedirectToPage("./Index");
        }

        private bool BlogExists(Guid id)
        {
            return _context.Blogs.Any(e => e.BlogId == id);
        }
    }

After the change of an element no new entry in the AutoHistorie table. This is strange…

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
xytingcommented, Jan 28, 2018

Yeah, maybe the README SHOULD be updated.

1reaction
xytingcommented, Jan 28, 2018

Oh, I’m sorry, _context.SaveChangesAsync(true); is my UnitOfWork feature, If you only use this nuget package, the correct usage is invoke _context.EnsureAutoHistory(); before await _context.SaveChangesAsync();

Tips: Your issue is your code invoke the _context.EnsureAutoHistory(); in EditModel constructor, but nothing changed in there. The correct is in your OnPostAsync() like following:

public async Task<IActionResult> OnPostAsync()
    {
        if (!ModelState.IsValid)
        {
            return Page();
        }

        _context.Attach(Blog).State = EntityState.Modified;

        try
        {
            // Ensure for generating the changing history
            _context.EnsureAutoHistory();
            // The changing history will insert into AutoHistories table
            await _context.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!BlogExists(Blog.BlogId))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return RedirectToPage("./Index");
    }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Is This Working?
There's no agreement about how teachers should discipline students. And there's evidence that some of the most popular punishments may harm ...
Read more >
Is This Working? - Hosted by Anna Codrea-Rado and ...
Listen to episodes and learn more about Is This Working?. A podcast about the messy parts of work, co-hosted by Anna Codrea-Rado and...
Read more >
Is This Working?
A podcast about the messy parts of work, co-hosted by Anna Codrea-Rado and Tiffany Philippou. ... Hosted on Acast. See acast.com/privacy for more...
Read more >
This American Life: “Is This Working?”
This podcast raises issues of racial inequality in school discipline and how teachers ought to discipline students. Topics/Themes: School discipline.
Read more >
Is This Working?: The Businesslady's Guide to Getting ...
Is This Working?: The Businesslady's Guide to Getting What You Want from Your Career: Guerra, Courtney C.W.: 9781440598494: Amazon.com: Books.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found