English 中文(简体)
实体框架4.1:挽救许多到许多关系的人,可避免在契约中再次割裂儿童实体。
原标题:Entity Framework 4.1: saving many to many relationship saves exsting child entities again in the db

我有一个非常令人沮丧的问题(对我来说,情况是:

我有一个简单的情景,即我有一个产品实体和一个合并项目实体。 它们之间有许多关系,因此产品可以属于多个合并产品,而合并产品可以包含许多产品。

我先创建一种产品,将其留给数据库。 我稍后将创造并增加这一产品。 当我试图挽救这一合并产品时,按实体框架将产品实体再次列入数据库,而不是仅仅增加关系。 这确实使我有营养。

I already tried to attach the product again to the context before saving, but Entity complains that it already has a product with the same key...

下面,你发现所有这些法典(简化和取消法典):

<>Productent>

Public Class SingleProduct
  Property SingleProductId As Integer
  Property CombinedProducts As ICollection(Of CombinedProduct)
End Class

<><>CombinedProduct>

Public Class CombinedProduct
  Public Sub New()
    Me.Products = New HashSet(Of SingleProduct)()
  End Sub

  Property CombinedProductId As Integer
  Property Products As ICollection(Of SingleProduct)
End Class

<>Many to many Relastship definition

Protected Overrides Sub OnModelCreating(modelBuilder As DbModelBuilder)
   modelBuilder.Entity(Of CombinedProduct)().
                        HasMany(Function(c) c.Products).
                        WithMany(Function(p) p.CombinedProducts).
                        Map(Sub(m)
                                m.ToTable("CombinedProductSingleProducts")
                                m.MapLeftKey("SingleProductId")
                                m.MapRightKey("CombinedProductId")
                            End Sub)
End Sub

www.un.org/Depts/DGACM/index_spanish.htm 储蓄法

Using myDataContext As New DataContext
  myDataContext.CombinedProducts.Add(product)
  myDataContext.SaveChanges()
End Using

www.un.org/Depts/DGACM/index_spanish.htm 在储蓄之前,曾经尝试过附加工作:

For Each prd In product.Products
   If myDataContext.Entry(prd).State = EntityState.Detached Then
      myDataContext.SingleProducts.Attach(prd)
   End If
Next

我发现,“溶剂”在储蓄、清除产品清单和再次从数据库中获取产品以及将产品添加到合并提款之前是正确的,但这不是解决办法。

Hope someone can help me, this is driving me nuts! (I use Entity Framework 4.1, with Code First)

<><>Edit>/strong>

www.un.org/Depts/DGACM/index_spanish.htm 添加产品: 采用某种其他方式,以自己的数据内容进行:

Using myDataContext As New DataContext
    myDataContext.SingleProducts.Add(singleProduct)
    myDataContext.SaveChanges()
End Using

www.un.org/Depts/DGACM/index_spanish.htm 合并产品创建:

Dim myCombinedProduct = New CombinedProduct
myCombinedProduct.Products.Add(product)

“我”补充说,产品在自己的数据内容中首当其冲:

Using myDataContext As New DataContext
    Return myDataContext.Products.FirstOrDefault(Function(p) p.ProductId = id)
End Using

<><>Edit>/strong>

The full story in the hopes of being more clear:

我采用了两种形式:一种是产品管理,另一种是合并产品管理。 该应用程序是N-Tier(User层、商业层和数据层)。

关于管理贵产品的形式,你只能补充/更新/替代产品。 在这种形式上,一切工作都是罚款的。

合并产品形式是另一个问题:

  1. when loading the form I retrieve all products from the database, going through the business and datalayer. The function to retrieve the products in the datalayer has it s own DataContext (using block). This function returns an iEnumerable of Products.
  2. The retrieved products are added to a number of comboboxes as objects.
  3. You select the products you want to add to the combined product by selecting them
  4. when saving I create a new CombinedProduct entity in the user layer, retrieve the product objects from the comboboxes and add them to the new CombinedProduct Object
  5. I send the CombinedProduct object to the business layer where I perform a number of business rules
  6. If all is well, the combinedProduct is send to the datalayer, where I try to save it again in it s own datacontext (using-block).

因此,我有多种数据背景,因为它们在数据层中生活和死亡。

希望使事情更加清晰。

最佳回答

In EF all entities that you are trying to relate must belong to the same datacontext,other wise it would not recognize your product as an existing one, and would add a new one instead in your first usings, your product must be fetched from db first.

我从未不得不对每个实体进行点击,但我看到大多数人会遇到这样的问题。

同样,在许多方面,这两个实体必须加入进来,关系只是要填表。

在研究讨论时,它认为你拥有每个实体类型的存放处,并且正在通过这些存放处方法获得物体。 如果情况如此,那么海事组织的这种设计就存在问题,因为物体关系储存在一个共同的环境中。 在你的情况下,所有这些存放处都应有相同的目录。 你可能认为,进入数据库有一些业绩问题,但即使你随附,你也会去掉。

问题回答

我根本不理解你在哪里采取什么步骤,但正确的秩序应当是:

如果一切都发生在一个背景下:

Using myDataContext As New DataContext
    Dim product = myDataContext.Products.FirstOrDefault(Function(p) p.ProductId = id)

    Dim myCombinedProduct = New CombinedProduct
    myCombinedProduct.Products.Add(product)

    myDataContext.CombinedProducts.Add(myCombinedProduct)
End Using

取代<代码><<>product/code>,随行并避免重复。 须在<><>之前>。 页: 1

如果您在另一个背景下装上product/code>:

Using myDataContext As New DataContext
    myDataContext.Products.Attach(product)

    Dim myCombinedProduct = New CombinedProduct
    myCombinedProduct.Products.Add(product)

    myDataContext.CombinedProducts.Add(myCombinedProduct)
End Using

您必须在此背景下附上<条码>产品/代码>。

<><>Edit>/strong>

如果您的新<代码>myCombinedProduct,包括产品采集deta <>>>>> /em>上填入数据传输器,说明应当做的工作:

Using myDataContext As New DataContext
    For Each prd In myCombinedProduct.Products
        myDataContext.SingleProducts.Attach(prd)
    Next

    myDataContext.CombinedProducts.Add(myCombinedProduct)
End Using




相关问题
Entity Framework with MySQL connector in c#

I have been trying to get the Entity Framework to work in my web application using MySQL. It works fine on my local pc, but doesn t work when I put it on the server. Since the server is a shared ...

How Do I Create And Update A Many To Many Relationship With EF

I am using the Entity Framework with SQL Server. I have a many to many relationship between 2 tables. I have created a join table with just the primary key fields of the 2 tables. In the designer, the ...

Entity Framework with File-Based Database

I am in the process of developing a desktop application that needs a database. The application is currently targeted to SQL Express 2005 and works wonderfully. However, I m not crazy about having ...

Linq to enties, insert foreign keys

I am using the ADO entity framework for the first time and am not sure of the best way of inserting db recored that contain foreign keys. this is the code that i am using, I would appreciate any ...

Entity Framework - Many to many question

I have a table called ASB and a table called PeopleInvolved. There is a junction table called PeopleInvolved_ASB which simply contains an ASBID and a PeopleInvolvedID column. The columns act as a ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

ADO.NET Entity Data Model are not precise enough

I run this code: var cos = from k in _db.klienci_do_trasy where k.klient_id == 5 select k; but the query send to database is: SELECT * FROM `klienci_do_trasy` LIMIT 0, 30 why is it for, there ...

热门标签