简体   繁体   English

使用EF将新的和更新现有对象添加到多对多表中

[英]Add new and update existed object into many-to-many table with EF

class A
{
    int Id; //Primary key
    string Name;
}

class B
{
    int Id; //Primary key
    string Name;
}

class AB
{
    int Id; //PK
    int AId; //FK
    int BId; //FK
}

In this situation: "A" exists in database "B" and "AB" are new created objects. 在这种情况下:“A”存在于数据库“B”中,“AB”是新创建的对象。

A a = new A("asd");
B b = new B("qwe");
AB ab = new AB(1, 1);

How should I add this with Entity Framework to database? 我应该如何将此实体框架添加到数据库?

Is it something like this? 这是这样的吗? It should be 3 context for each entity? 每个实体应该是3个上下文?

mydb.A.Context.ApplyCurrentValues(a)
mydb.B.Context.AddObject(b)
mydb.AB.Context.AddObject(ab) //connected with A and B above

In a database first approach, firstly you entity AB unnecessarily has a primary key as its an associative entity only. 在数据库第一种方法中,首先,实体AB不必要地将主键作为其关联实体。 Unless you want to hold other attributes on that table a Pk is not necessary and would result in EF 4 creating a separate entity. 除非您希望在该表上保留其他属性,否则不需要Pk,这将导致EF 4创建单独的实体。

Db Diagram of relationship: Db关系图:

在此输入图像描述

EF edmx result: EF edmx结果:

在此输入图像描述

Using that relationship to answer your question. 使用这种关系来回答你的问题。

    public void addNewBtoA(B newEntity, A existingEntity) {

        _myContext.Attach(existingEntity);
        existingEntity.B.Add(newEntity);
        myContext.SaveChanges();

    }

EDIT: 编辑:

An AB record is automatically created by EF in the Db for the many to many relationship. EF在Db中为多对多关系自动创建AB记录。

EDIT 2: 编辑2:

If entity AB has other properties on it then EF will import it as a separate entity, updating the context would go summing like this: 如果实体AB上有其他属性,那么EF会将其作为一个单独的实体导入,更新上下文会像这样求和:

B b = new B {
    Name = "name"
};

AB ab = new AB {

    A = existingA,
    B = b,
    arbitraryProp = "my heart is a fist of blood" 

}

_myContext.Attach(existingA);
_myContext.Add(b);
_myContext.Add(ab);
_myContext.SaveChanges();

The order of the add is irrelevant as EF determines the order of insertion based on the model relationships defined in the .edmx. 添加的顺序无关紧要,因为EF根据.edmx中定义的模型关系确定插入顺序。 Remember that attaching existingA is only necessary if existingA is not currently tracked by the _myContext - ie if existingA was fetched using _myContext.A.SingleOrDefault() then it is already attached. 请记住,安装existingA只是必要的,如果existingA当前不被跟踪_myContext -也就是说,如果existingA是牵强使用_myContext.A.SingleOrDefault()然后它已连接。

EDIT 3: 编辑3:

public void testab() {
            A existingA = new A {
                Name = "test a"
            };

            using (ABEntities context = new ABEntities()) {
                context.A.AddObject(existingA);

                context.SaveChanges();
            }


            using (ABEntities context = new ABEntities()) {
                B newB = new B {
                    Name = "test b"
                };

                context.Attach(existingA);
                existingA.B.Add(newB);
                context.SaveChanges();
            }
        }

You need modify your AB class definition as follows 您需要修改您的AB类定义,如下所示

class AB
{
    public int Id { get; set; }
    public virtual A A { get; set; }
    public virtual B B { get; set; }
}

Then 然后

AB ab = new AB { A = a, B = b };

// if 'a' is not attached call mydb.A.Attach(a);
mydb.AB.AddObject(ab);

EF will figure out the insertion order of the entities. EF将确定实体的插入顺序。

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

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