0

I've created custom fields to the User:Identity class

public class User : IdentityUser
{        
    public virtual ICollection<Item> Items { get; set; }      
}  

where Item is a custom class

public class Item
{
    public Guid Id { get; set; }                
    public string OwnerId { get; set; }
    public string Name { get; set; }

    [ForeignKey("OwnerId")]
    public virtual User User { get; set; }    
}

Then I use it in a controller method

    [HttpPost]
    public ActionResult AddToFavourite(bool check)
    {
        var user = UserManager.FindByIdAsync(User.Identity.GetUserId()).Result;
        var item = new Item();

        if (check)
            user.Items.Add(item);            
        else
            user.Items.Remove(item);

        _context.CommitChanges();

        return View();
    }

Can anybody explain how to save this changes in database, please? I was trying to implement solution according to this, but unfortunately it still doesn't work for me. User entity is updated, but no changes saved in database.

UPDATE

public class ApplicationDbContext : IdentityDbContext<User>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    { }
    public DbSet<Item> Items { get; set; }      

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder); 
        modelBuilder.Entity<User>().HasMany(p => p.Items).WithMany().Map(mp => mp.ToTable("Favourite"));           
    }
}

public class Repository : IDisposable
{
    private ApplicationDbContext _dbContext = new ApplicationDbContext();

    public void CommitChanges() => _dbContext.SaveChanges();        

    private bool disposed = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                _dbContext.Dispose();
            }
        }
        this.disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}
Egor
  • 153
  • 1
  • 2
  • 10

1 Answers1

0

You need to instantiate a database connection. I assume you either have got down a database first route or code first and have the connection set up. If you go to your web.config you will see the connection string.

You can instantiate the database connection by using a 'using statement' this will dispose the connection once the using statement has compiled.

using(var db = new DatabaseEntities())
{
    var user = UserManager.FindByIdAsync(User.Identity.GetUserId()).Result;
    var item = new Item();

    if (check)
        user.Items.Add(item);   
    else
        user.Items.Remove(item);



    db.SaveChanges();

}

However, this wont save anything to the database regardless, because your code isnt applying any changes to the database - you are using in memory data such as user.items.

  • Kallen Newick, many thanks for your answer. Actually I have a generic repository class with CommitChanges() method (please see UPDATE). It saves changes for my entities, but it doesn't work for User : Identity class. The same for Microsoft.AspNet.Identity.UserManager.UpdateAsync(user) method. So I don't understand what is a root cause of the issue – Egor Feb 15 '18 at 13:14