简体   繁体   English

这是实现与EF代码首先多对多关系的集合的方法吗?

[英]Is this the way to implement a collection participating in and EF code first many to many relationship?

I have a situation where the code I've arrived at doesn't match any examples I find so I wonder if I'm missing something. 我遇到的情况是我到达的代码与我找到的任何示例都不匹配,因此我想知道是否丢失了某些东西。

Basically, I want an EF code first Entity that contains a collection of Entities participating in a many-to-many relationship. 基本上,我想要一个EF代码优先的Entity,其中包含参与多对多关系的实体的集合。

Then, I'd like to be able to: 然后,我希望能够:

  • Add to collection at the same time as creating an entity 在创建实体的同时添加到集合
  • Not get a warning about accessing a virtual member from constructor 没有收到有关从构造函数访问虚拟成员的警告

Here's what I have: 这是我所拥有的:

public class NotificationUser
{
    private ICollection<NotificationUserGroup> _userGroups = new HashSet<NotificationUserGroup>();

    public int UserId { get; set; }

    public string UserName { get; set; }

    public bool IsActive { get; set; }

    public virtual ICollection<NotificationUserGroup> UserGroups
    {
        get { return _userGroups; }
        set { _userGroups = value; }
    }
}

Is there a better/different way to accomplish my goal? 有没有更好/不同的方式来实现我的目标?

This example might help 这个例子可能有帮助

public class NotificationUser
{
    public NotificationUser()
    {
        UserGroups = new HashSet<NotificationUserGroup>();
    }

    public int NotificationUserId { get; set; }
    public string UserName { get; set; }
    public bool IsActive { get; set; }
    public virtual ICollection<NotificationUserGroup> UserGroups { get; set; }
}

public class NotificationUserGroup
{
    public int NotificationUserGroupId { get; set; }
    public string GroupName { get; set; }
}

public class Context : DbContext
{
    public Context()
        : base()
    {

    }
    public DbSet<NotificationUser> NotificationUsers { get; set; }
    public DbSet<NotificationUserGroup> NotificationUserGroup { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        Database.SetInitializer(new DropCreateDatabaseAlways<Context>());

        using (var ctx = new Context())
        {
            var user = new NotificationUser() { UserName = "Name1" };
            user.UserGroups.Add(new NotificationUserGroup() { GroupName = "Group1" });
            user.UserGroups.Add(new NotificationUserGroup() { GroupName = "Group2" });
            ctx.NotificationUsers.Add(user);

            ctx.SaveChanges();
        }

        using (var ctx = new Context())
        {
            foreach (var user in ctx.NotificationUsers)
            {
                foreach (var group in user.UserGroups)
                    Console.WriteLine("Group Id: {0}, Group Name: {1}, UserName: {2}", group.NotificationUserGroupId, group.GroupName,user.UserName);
            }

            foreach (var group in ctx.NotificationUserGroup)
            {
                Console.WriteLine("Group Id: {0}, Group Name: {1}", group.NotificationUserGroupId, group.GroupName);
            }
        }

        Console.ReadKey();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM