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

问题描述 投票:0回答:1

我试图将列表保存到数据库,但是,它没有任何效果。

这是我的模型:

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

我的错误在哪里?

c# asp.net
1个回答
0
投票

数据库架构大纲:

表:

  • dbo.Users
  • 赌博.games
  • dbo.UsersInGame
  • dbo.ChatSession

外键:

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

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

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; }
}
© www.soinside.com 2019 - 2024. All rights reserved.