【问题标题】:EF Core - Added element of relation isnt being saved by EFEF Core - 添加的关系元素没有被 EF 保存
【发布时间】:2019-01-01 23:03:54
【问题描述】:

我在将新对象添加到用户类(关系)中的集合时遇到问题,一对多,一个用户可以拥有多个项目

但由于某种原因,上下文没有将其正确保存在用户中。

这段代码的结果:

  • 用户被添加到 _context.UsersTable 并且可以从其他控制器读取

  • Item 已添加到 _context.ItemsTable(并且可以从其他控制器读取),但 User 和 Item 之间没有任何联系。

  • 用户的项目集合始终为空。

    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();
    }
    

【问题讨论】:

    标签: c# entity-framework entity-framework-core asp.net-core-webapi


    【解决方案1】:

    你的 Item 类是错误的,你需要这个:

    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;
        }
    }
    

    还有注册方法:

    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();
    }
    

    【讨论】:

    • 我更改了您的代码,但仍然没有添加项目。集合为空。
    • 您是否检查过是否在数据库中创建了项目?
    • 在我想要返回我所做的项目列表的其他端点pastebin.com/c8ZuBDxc 并且它返回“TEST”,所以是的 - 项目在数据库中但未连接到用户。
    • 此外,“项目”在数据库中有正确的用户 GUID。
    • 所以项目存在于数据库中,但不存在于用户的集合中。
    猜你喜欢
    • 2016-12-19
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 2021-11-25
    • 2021-05-01
    • 1970-01-01
    • 2021-09-15
    • 2020-10-03
    相关资源
    最近更新 更多