实体框架将记录到多对多的映射表 [英] Entity Framework adding record into many to many mapping table

查看:120
本文介绍了实体框架将记录到多对多的映射表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个表,

1),客户(编号,名称,BLA BLA) 2)CustomerGroups(GroupId的,组名) 3)CustomerInGroups(客户ID,的GroupId)

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;
}

我如何添加一条记录CustomerInGroups?的EntityFramework不会产生这样的许多一对多映射表实体

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

编辑:

无论是在客户和CustomerGroups的Id列设置为自动递增。

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

所以在我CustomersGroup表,我有

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

我得到的是

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

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

它实际上创造了我CustomerGroups表中的另一个入口,这是不是我想要的。

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

推荐答案

飞有点盲目的因为你没有包含什么样的属性实体有。但是,你应该有一个关系的属性 CustomerGroups 。只需设置你要进行相关的团体属性。例如,这将创建一个新的组名称为foo栏和相关的实体组。

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;
}

如果关系是正确设置,EF会自动记录插入 CustomerGroups 并插入一个关系到 CustomerInGroups 表。

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

编辑:

如果你想添加现有 CustomerGroup 来新的客户。你想获得 CustomerGroup 从数据库中第一,然后将其添加到您所插入的Customer实体。

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;
}

这篇关于实体框架将记录到多对多的映射表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆