如何将列表保存到数据库?

时间:2019-03-07 18:36:22

标签: c# asp.net

我正在尝试将列表保存到数据库,但是,它没有任何作用。

这是我的模特:

    private IList<int> _chatIDs;
    public IList<int> ChatIDs
    {
        get { return _chatIDs ?? (_chatIDs = new List<int>()); }
        set { _chatIDs = value; }
    }

这是最省钱的部分:

    MyDbContext myDbContext = new MyDbContext();
    var game = myDbContext.Events.Find(e.ID);
    var newList = game.ChatIDs;
    newList.Add(e.ChatID);
    game.ChatIDs = newList;
    myDbContext.SaveChanges();

我的错误在哪里?

1 个答案:

答案 0 :(得分:0)

数据库架构概述:

表格:

  • dbo.Users
  • dbo.Games
  • dbo.UsersInGame
  • dbo.ChatSession

外键:

  • dbo.UsersInGame.UserId> dbo.Users.UserId
  • dbo.UsersInGame.GameId> dbo.Games.GameId
  • dbo.Games.ChatId> dbo.ChatSessions.ChatId

然后在模型中为这些外键关系创建集合,如下所示(很抱歉,我花时间编写示例代码,我只是从我当前项目之一的生成实体中获取了代码):< / p>

public partial class SecurityUser
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public SecurityUser()
        {
            this.SW_Mobile_Pick_Assignment = new HashSet<SW_Mobile_Pick_Assignment>();
            this.SW_Mobile_Session = new HashSet<SW_Mobile_Session>();

        }

        public int SecurityUserId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Login { get; set; }
        public string Passwrd { get; set; }
        public string Email { get; set; }
        public bool Enable { get; set; }
        public string FileMakerId { get; set; }
        public string Phone { get; set; }
        public string Fax { get; set; }
        public Nullable<bool> IsAccountManager { get; set; }
        public Nullable<bool> IsHandHeldAccount { get; set; }
        public Nullable<int> HandheldPin { get; set; }
        public string Last5SSN { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<SW_Mobile_Session> SW_Mobile_Session { get; set; }
}