Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

c# - Entity Framework adding record into many to many mapping table

I have 3 tables,

1) Customer (Id, Name, bla bla)

2) CustomerGroups (GroupId, GroupName)

3) CustomerInGroups (CustomerId, GroupId)

using (var context = DataObjectFactory.CreateContext())
{                
    context.Customers.Add(entity);
    context.SaveChanges();
    return entity.Id;
}

How do I add a record into CustomerInGroups? EntityFramework doesn't generate entities for such many-to-many mapping tables

Edit:

Both the Id columns in Customer and CustomerGroups are set to auto increment.

So in my CustomersGroup table, I have

Id          Name
----------------------------
1           Normal
2           VIP

I tried doing this as one of the posters suggested:

entity.CustomerGroups = new List<CustomerGroup>
{
    new CustomerGroup {Id = 2 }
};
context.Customers.Add(entity);
context.SaveChanges();
return entity.Id;

However, when I did this, instead of creating a record in the mapping table like this:

CustomerId          GroupId
----------------------------
1                   2

What I got was

CustomerInGroups
    CustomerId          GroupId
    ----------------------------
    1                   3

CustomerGroups
    Id          Name
    ----------------------------
    1           Normal
    2           VIP
    3           NULL

It actually created another entry in my CustomerGroups table, which is not what I want

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Flying a little blind since you didn't include what properties entity has. But you should have a property for the relationship to CustomerGroups. Just set that property with the groups you want to be related to. For example, this would create a new Group name "foo bar" and relate the entity to that group.

using (var context = DataObjectFactory.CreateContext())
{
    entity.CustomerGroups = new List<CustomerGroup> { GroupName = "Foo bar" };
    context.Customers.Add(entity);
    context.SaveChanges();
    return entity.Id;
}

If the relationship is setup correctly, EF will automatically insert a record into CustomerGroups and insert a relationship into the CustomerInGroups table.

EDIT:

If you're trying to add an existing CustomerGroup to a new Customer. You'll want to get the CustomerGroup from the database first, then add it to the Customer entity you're inserting.

using (var context = DataObjectFactory.CreateContext())
{
    var customerGroups = context.CustomerGroups.Where(...).ToList(); // get your CustomerGroup object(s) here, and ensure it's enumerated with ToList()
    entity.CustomerGroups = customerGroups;
    context.Customers.Add(entity);
    context.SaveChanges();
    return entity.Id;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...