Search code examples
c#asp.net-coreasp.net-core-mvcasp.net-identity

Why is EF trying to insert currently logged in user?


I was trying to add user authentication to an existing application. While trying to add a new product, I am getting below error:

"MySqlException: Duplicate entry 'a82c1468-b942-4c48-a787-defdc584641d' for key 'PRIMARY'".

I am using EF Core 1.1.2

This is my ApplicationDbContext:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions options) : base(options)
    {
    }

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

    public virtual DbSet<Review> Reviews { get; set; }
    public virtual DbSet<Product> Products { get; set; }
}

Here is my Startup class:

public class Startup
{
    public IConfigurationRoot Configuration { get; set; }

    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json");
        Configuration = builder.Build();
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddEntityFrameworkMySql()
                .AddDbContext<ApplicationDbContext>(options =>
                              options.UseMySql(Configuration["ConnectionStrings:DefaultConnection"]));

        // This is for Identity
        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseIdentity();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Account}/{action=Index}/{id?}");  // <-There is an edit here
        });//there is a change here for idenity

        app.Run(async (context) =>
        {
            await context.Response.WriteAsync("Hello World!");
        });
    }
}

Here is my Create action method that insert new product to the database:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(Product product, ICollection<IFormFile> files = null)
{
    if (files != null)
    {
        foreach (var file in files)
        {
            if (file.Length > 0)
            {
                using (MemoryStream ms = new MemoryStream())
                {
                    file.CopyTo(ms);
                    byte[] fileBytes = ms.ToArray();
                    product.ProductImg = fileBytes;
                }
            }
        }
    }

    var userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
    var currentUser = await _userManager.FindByIdAsync(userId);
    product.User = currentUser;

    if (ModelState.IsValid)
    {
        _context.Save(product);
        return RedirectToAction("Index");
    }

    return View(product);
}

Here are my Register and Login methods:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
    ViewData["ReturnUrl"] = returnUrl;

    if (ModelState.IsValid)
    {
        Microsoft.AspNetCore.Identity.SignInResult result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, isPersistent: true, lockoutOnFailure: false);

        if (result.Succeeded)
        {
            _logger.LogInformation(1, "User logged in.");
            return RedirectToLocal(returnUrl);
        }
        else
        {
            ModelState.AddModelError(string.Empty, "Invalid login attempt.");
            return View(model);
        }
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
{
    ViewData["ReturnUrl"] = returnUrl;

    if (ModelState.IsValid)
    {
        var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
        var result = await _userManager.CreateAsync(user, model.Password);

        if (result.Succeeded)
        {
            _logger.LogInformation(3, "User created a new account with password.");
            return RedirectToLocal(returnUrl);
        }
    }

    return View(model);
}

This is my Product model class:

public class Product
{
    [Key]
    public int ProductId { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    [DisplayFormat(DataFormatString = "{0:c}")]
    public double Cost { get; set; }

    [Required]
    [Display(Name = "Country of Origin")]
    public string CountryOfOrigin { get; set; }

    [Display(Name = "Product Image")]
    public byte[] ProductImg { get; set; }

    [DisplayFormat(DataFormatString = "{0:d}")]
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime DatePosted { get; set; }

    public virtual ApplicationUser User { get; set; }
    public virtual ICollection<Review> Reviews { get; set; }
}

Solution

  • Entity Framework can not tell that the User you are attaching to the product already exists. There are a couple of ways to handle this:

    1) Explicitly set the state to Unchanged:

    context.Entry(product.User).State = EntityState.Unchanged; 
    

    2) Expose the FK UserId on your product entity. See here

    product.UserId = userId