代码之家  ›  专栏  ›  技术社区  ›  koviroli

在ASP.NET中存储带有EF的字符串集合

  •  0
  • koviroli  · 技术社区  · 8 年前

    我在一家 ASP.NET MVC Core 2.0

    List<string> string[] ,因为EF无法映射这些类型的集合。

    ApplicationUser 一个ASP.NET项目。)

    我已经考虑过将所有ID存储在一个简单的字符串中,用任何符号(例如分号)将其拆分,但有一个问题:一个简单的字符串可以有多长时间,在那里我得到了一个我读过的答案:

    可能是做错了什么。”]是这样的。

    1 回复  |  直到 7 年前
        1
  •  2
  •   John White    7 年前

    很难缠,没用,但很准确。这是一个典型的m:m场景。

    public class Topic {
        public int TopicId { get; set; }
        // other topic stuff
        public virtual User Owner { get; set; }
        public virtual ICollection<Collaboration> Collaborators { get; set; }
    }
    
    public class User {
        public int UserId { get; set; }
        // other user stuff
        public virtual ICollection<Collaboration> Collaborators { get; set; }
    }
    
    public class Collaboration {
        public int CollaborationId { get; set; }
        // other stuff dependent on topic + user
        public int UserId { get; set; }
        public virtual User User { get; set; }
        public int TopicId { get; set; }
        public virtual Topic Topic { get; set; }
    }
    

    db.Topics.Include("Collaborators").Include("Collaboration.User").FirstOrDefault(p => p.TopicId == <key value>);
    

    用户可以编辑的所有主题

    db.Users.Include("Collaborators").Include("Collaboration.Topic").FirstOrDefault(p => p.UserId == <key value>);