EF Core - Added element of relation isnt being saved by EF

asp.net-core-webapi c# entity-framework entity-framework-core

Question

I have problem with adding new object's to collection inside user class (relation) one-to-many, one user can have many items

But for some reason context is not saving it properly inside User.

Result of this code:

  • User is added to the _context.UsersTable and can be read from other controllers

  • Item is added to the _context.ItemsTable (and can be read from other controllers), but there's no connection between User and Item.

  • Items collection at user is always empty.

    public class User
    {
        [Key]
        public Guid Id { get;  set; }
        public virtual ICollection<Item> Items { get; set; } = new HashSet<Items>();
        protected User()
        {
    
        }
        public User(string login, string password)
        {
            (...)
        }
    }
    
    public class Item
    {
        [Key]
        public virtual User User { get; set; }
        public string ItemName { get; set; }
    
        protected Item()
        {
    
        }
    
        public Item(string name, User user)
        {
            Id = new Guid();
            this.User = user;
            ItemName = name;
        }
    }
    
    public class AppContext : DbContext
    {
        public AppContext(DbContextOptions<AppContext> options) : base(options)
        {
            Database.SetCommandTimeout(35000);
        }
    
        public DbSet<User> UsersTable { get; set; }
        public DbSet<Items> ItemsTable { get; set; }
    }
    
    
    
    public IActionResult register([FromBody]Register command)
    {
        var newUser = new User(command.login, password);
        var newItem = new Item("TEST", newUser);
    
        _context.Entry(newUser).State = EntityState.Modified;
    
        newUser.Items.Add(newItem);
    
        _context.UsersTable.Add(newUser);
    
        _context.ItemsTable.Add(newItem);
    
        _context.SaveChanges();
    }
    
1
0
7/25/2018 7:53:35 AM

Accepted Answer

Your Item class is wrong, you need this :

public class Item
{
    [Key]
    public Guid Id { get; set; }

    public string ItemName { get; set; }

    // Relation to User
    public Guid UserId { get; set; }
    public virtual User User { get; set; }

    protected Item()
    {    }

    public Item(string name, User user)
    {
        Id = Guid.NewGuid();
        UserId = user.Id;
        ItemName = name;
    }
}

And the register method :

public IActionResult register([FromBody]Register command)
{
    var newUser = new User(command.login, password);
    var newItem = new Item("TEST", newUser);

    newUser.Items.Add(newItem);
    _context.UsersTable.Add(newUser);
    _context.ItemsTable.Add(newItem); // Edit : I'm not sure, but try it without this line, it should work
    _context.SaveChanges();
}
1
7/25/2018 8:16:01 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow