Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Autohistory #38

Open
mev9669 opened this issue Oct 3, 2019 · 1 comment
Open

Custom Autohistory #38

mev9669 opened this issue Oct 3, 2019 · 1 comment

Comments

@mev9669
Copy link

mev9669 commented Oct 3, 2019

I have updated my project to .Net core 3.0.

Following the article, I did the below in my MVC web application project

class CustomAutoHistory : AutoHistory
{
    public int UserId { get; set; }
}

Then register it in the db context like follows:
modelBuilder.EnableAutoHistory<CustomAutoHistory>(o => { });

Then in my MVC controller code I did below

  if (ModelState.IsValid)
            {
                try
                {
                    _context.EnsureAutoHistory(() => new CustomAutoHistory()
                    {
                        UserId = Convert.ToInt16( User.FindFirstValue(ClaimTypes.NameIdentifier))
                     });
                    _context.Update(cust);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ClientExists(cust.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
            }

CustomAutoHistory table script

CREATE TABLE [dbo].[|CustomAutoHistory](
[Id] [int] IDENTITY(1,1) NOT NULL,
[RowId] nvarchar NOT NULL,
[TableName] nvarchar NOT NULL,
[Changed] nvarchar NULL,
[Kind] [int] NOT NULL,
[Created] datetime2 NOT NULL,
[UserId] [int] NULL,
CONSTRAINT [PK_CustomAutoHistory] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

The customer records gets updated but there is no record in the Custom History table. Can you please check the custom AutoHistory?

@duhowise
Copy link

Change the order of operations like so:

if (ModelState.IsValid)
{
    try
    {
        _context.Update(cust);
        _context.EnsureAutoHistory(() => new CustomAutoHistory()
        {
            UserId = Convert.ToInt16(User.FindFirstValue(ClaimTypes.NameIdentifier))
        });
        await _context.SaveChangesAsync();
    }
    catch (DbUpdateConcurrencyException)
    {
        if (!ClientExists(cust.Id))
        {
            return NotFound();
        }
        else
        {
            throw;
        }
    }
}

and try again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants