English 中文(简体)
以多对多关系填充链接表
原标题:Populating a linking table in a many-to-many relationship

我正在努力掌握EF4 CTP5。我有两个类具有多对多关系:Member和MemberGroup。CTP5代码首先生成了两个表(Members和MemberGroups),还有第三个名为MemberGroupMembers的表,它有两列(MemberGroupId和MemberId)。到目前为止,一切都如我所期望的那样。我已经为数据库添加了一些Members和MemberGroups。问题是,我找不到如何将一个或多个MemberGroup分配给某个成员,这将导致在MemberGroupMembers表中为分配给该成员的每个MemberGroup插入一行。

    public class Member
{
    public int Id { get; set; }

    public Guid SecureId { get; set; }

    public DateTime JoinedOn { get; set; }

    public virtual ICollection<MemberGroup> MemberGroups { get; set; }
}

public class MemberGroup
{
    public int Id { get; set; }

    public string Name { get; set; }

    public DateTime CreatedOn { get; set; }

    public virtual ICollection<Member> Members { get; set; }
}

public class CTP5testContext : DbContext
{
    public CTP5testContext() : base("CTP5test") { }

    public DbSet<Member> Members { get; set; }

    public DbSet<MemberGroup> MemberGroups { get; set; }
}

public class CTP5testContextInitializer : DropCreateDatabaseIfModelChanges<CTP5testContext>
{
    protected override void Seed(CTP5testContext context)
    {
        new List<Member>
        {
            new Member
            {
                Id = 1,
                SecureId = Guid.NewGuid(),
                JoinedOn = DateTime.Now
            }
            ,
            new Member
            {
                Id = 2,
                SecureId = Guid.NewGuid(),
                JoinedOn = DateTime.Now
            }
        }.ForEach(m => context.Members.Add(m));

        var memberGroup = new MemberGroup()
        {
            Id = 1,
            Name = "MemberGroup 1",
            CreatedOn = DateTime.Now
        };

        context.MemberGroups.Add(memberGroup);

        // How can I assign Member 1 to MemberGroup 1 ?????

        context.SaveChanges();
    }
}

我希望我在这里要做的事情很清楚,有人能给我一个如何实现这一目标的例子。

Regards, Erwin

最佳回答

您必须使用在POCO类中定义的集合。因此,如果您想将member1分配给memberGroup,只需调用:

memberGroup.Members.Add(member1);
问题回答

暂无回答




相关问题
Multiple @ManyToMany sets from one join table

I m mapping a proprietary database to Hibernate for use with Spring. In it, there are a couple of jointables that, for entity A and entity B have the following schema: CREATE TABLE AjoinB ( idA int ...

Many to Many relationship with Fluent NHibernate

I m getting the following error: "Can t figure out what the other side of a many-to-many should be." Team Entity: public class Team : IEntity { public int Id { get; set; } public string Name ...

select_related() and many to many fields with ajax (Django)

I have a simple view that I want to respond to both ajax and regular HTTP requests. Simplified, it looks like this: def tag_search(request, tag): items = Item.objects.filter(tags__tagname__exact=...

Visual Studio 2008 Class Designer - many to many associations

Is there a trick for visualizing a many to many assocation in the Visual Studio Class Designer? I have an Employee entity and a Team entity. So each entity has a list of his counterpart. Associations ...

Exclude on a many-to-many relationship through a third table

I have a problem making "exclude" querys on tables which have a many-to-many relationship through a third table. I have a table with projects, a table with people and a relationsship table with the ...

热门标签