我有一个非常令人沮丧的问题(对我来说,情况是:
我有一个简单的情景,即我有一个产品实体和一个合并项目实体。 它们之间有许多关系,因此产品可以属于多个合并产品,而合并产品可以包含许多产品。
我先创建一种产品,将其留给数据库。 我稍后将创造并增加这一产品。 当我试图挽救这一合并产品时,按实体框架将产品实体再次列入数据库,而不是仅仅增加关系。 这确实使我有营养。
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层、商业层和数据层)。
关于管理贵产品的形式,你只能补充/更新/替代产品。 在这种形式上,一切工作都是罚款的。
合并产品形式是另一个问题:
- 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.
- The retrieved products are added to a number of comboboxes as objects.
- You select the products you want to add to the combined product by selecting them
- 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
- I send the CombinedProduct object to the business layer where I perform a number of business rules
- 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).
因此,我有多种数据背景,因为它们在数据层中生活和死亡。
希望使事情更加清晰。