简体   繁体   English

在多对多数据库Sql Server中的过渡表中添加项目和更新关系

[英]Add item and update relationship in transitional table in many-to-many database Sql Server

--here was wrong model without association manyTOmany between AB. -这是错误的模型,没有AB之间的许多关联。 corrected is in EDIT2-- 更正于EDIT2--

A exists in database, B exists in database. 数据库中存在A ,数据库中存在B I need only enter new C element with some Properties1 and Properties2 (and update collections of C in existed A and B elements) 我只需要输入带有一些Properties1和Properties2的新C元素(并更新现有AB元素中C集合)

I tried many options, like for example this, but still somethings wrong (with ObjectOCntext and existed Key etc) 我尝试了很多选项,例如这样,但是还是有些错误(使用ObjectOCntext和存在的Key等)

void SaveNewC(C newC)
{
    using (var context = new MyEntities(connectionString))
    {
        var dbA = context.A.Where(a => a.Id == newC.A.Id).SingleOrDefault();
        var dbB = context.B.Where(b => b.Id == newC.B.Id).SingleOrDefault();

        newC.A = dbA;
        newC.B = dbB;
        context.AddObject(newC);

        context.SaveChanges();
    }
}

EDIT 编辑

Exception that I get: "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key." 我得到的异常是: "An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key."

EDIT2 updated model EDIT2更新了模型

在此处输入图片说明

context.Entry(dbA).State = EntityState.Unchanged;
context.Entry(dbB).State = EntityState.Unchanged;
context.AddObject(newC);
context.SaveChanges();

Apparently your newC has already populated navigation properties A and B with the correct Id s. 显然,您的newC已经使用正确的Id填充了导航属性AB Then you can just attach the entities refered by the navigation properties to the context: 然后,您可以将导航属性引用的实体附加到上下文:

void SaveNewC(C newC)
{
    using (var context = new MyEntities(connectionString))
    {
        context.A.Attach(newC.A);
        context.B.Attach(newC.B);

        context.C.AddObject(newC);

        context.SaveChanges();
    }
}

Do you need to have the junction table mapped? 您是否需要映射联结表? If all your wanting is a Many to Many from A->B you could do it in a simpler way. 如果您想要的只是A-> B中的“多对多”,则可以用一种更简单的方式来实现。

If you created the C table as a true junction - and have a FK to A and B set to it's PK in sql like this: 如果您将C表创建为真正的联结-并且在SQL中将A和B的FK设置为​​它的PK,如下所示:

在此处输入图片说明

Then when you create your edmx from the model it will create this: 然后,当您从模型创建edmx时,将创建以下内容:

在此处输入图片说明

Now, in your code if you wanted to add a relationship, you would simply add it to your collection, and EF will automatically create the relationship in your C table for you: 现在,在代码中,如果您想添加关系,只需将其添加到集合中,EF就会在您的C表中自动为您创建关系:

        var A = new A();
        var b = new B();
        var b2 = new B();

        A.B.Add(b);
        A.B.Add(b2);

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

相关问题 如何在Entity Framework Core中将项目添加到多对多关系表中? - How to add an item to a many-to-many relationship table in Entity Framework Core? 如何在MVC中添加多对多关系的联结表? - How to add to junction table of many-to-many relationship in MVC? LINQ to SQL 查询多对多关系 - LINQ to SQL Query for Many-to-Many relationship 使用EF将新的和更新现有对象添加到多对多表中 - Add new and update existed object into many-to-many table with EF 这是多对多关系吗? - Is this a many-to-many relationship? 如果数据库没有链接表,如何在Entity Framework中创建多对多关系? - How do you create a many-to-many relationship in Entity Framework if the database doesn't have a linking table? 在实体框架中添加具有多对多关系的项目 - Adding Item with Many-to-Many Relationship In Entity Framework NHibernate - 如何从多对多关系中删除项目? - NHibernate - how to delete an item from many-to-many relationship? C#Nhibernate-将新项目添加到多对多关系 - C# Nhibernate - Adding new item to many-to-many relationship 通过具有多对多关系的实体框架代码优先方法为 SQL Server 播种 - Seeding SQL Server by Entity Framework code-first approach with many-to-many relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM