인벤토리, InventoryLocations, InventoryCounts의 세 가지 테이블이 있습니다. 인벤토리에는 항목 설명이 있습니다. InventoryLocations는 위치 설명을 보유합니다. InventoryCounts는 해당 위치의 InventoryID, LocationID 및 항목 수를 가진 브리지 테이블입니다.컨텍스트를 먼저 저장하지 않고도 Entity Framework에서 서로 참조하는 여러 개의 새 항목을 추가 할 수 있습니까?
파일을 가져올 때 모두 3 개가 새 파일 일 수 있습니다. 따라서 컨텍스트 내에서 인벤토리 항목을 추가하고 DB/EF가 InventoryID를 생성하도록하십시오. 그런 다음 DB에서 ID를 생성하도록 위치 설명을 추가하려고합니다. 그런 다음 위치에 대해 생성 된 ID뿐만 아니라 Inventory 항목에 대해 생성 된 ID를 사용하여 InventoryCounts 테이블에 새 레코드를 추가하십시오. 하나의 컨텍스트와 하나의 SaveChanges 만 사용하여이 작업을 수행하려고합니다.
내가 읽은 것부터는이 모든 것을 할 수있는 것처럼 보이지만이 작업을 수행하려면 Navigation 속성이나 Fluent API에 대해 충분히 알지 못합니다. 아래 코드를 사용하면 작동하지 않습니다.
데이터를 분석하여 모든 위치를 가져온 다음 저장하는 것이 좋습니다. 그런 다음 모든 인벤토리를 가져온 다음 저장하십시오. 그런 다음 카운트를 추가하십시오. 이것은 비효율적입니다.
샘플 코드는 다음과 같습니다 거트 위에서 제안한 것처럼
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel.DataAnnotations
Imports System.ComponentModel.DataAnnotations.Schema
Imports System.Data.Entity.Spatial
Public Class Test
'' Test Class
Public Sub test()
Using ctx As New PIOCContext
Dim Inv = ctx.App_Inventory
Dim Loc = ctx.App_InventoryLocations
Dim InvCounts = ctx.App_InventoryCounts
Dim i As New App_Inventory() With {.ItemName = "Widget"}
Inv.add(i)
Dim l As New App_InventoryLocation() With {.Location = "Foo"}
Loc.Add(l)
Dim cnt As New App_InventoryCount() With {.InventoryID = i.InventoryID, .Qty = 1, .QtyUOM = "Each", .LocationID = l.LocationID}
InvCounts.Add(cnt)
ctx.SaveChanges()
End Using
End Sub
End Class
'' Models
<Table("dev.App_InventorySubLocations")>
Partial Public Class App_InventoryLocation
<Key>
<Column(TypeName:="uint"), DatabaseGenerated(DatabaseGeneratedOption.Identity)>
Public Property LocationID As Long
<Required>
<StringLength(45)>
Public Property Location As String
End Class
<Table("dev.App_InventoryCounts")>
Partial Public Class App_InventoryCount
<Key>
<Column(Order:=0, TypeName:="uint")>
<DatabaseGenerated(DatabaseGeneratedOption.None)>
Public Property InventoryID As Long
<Key>
<Column(Order:=1, TypeName:="uint")>
<DatabaseGenerated(DatabaseGeneratedOption.None)>
Public Property LocationID As Long
<Column(TypeName:="uint")>
Public Property Qty As Long
<Required>
<StringLength(45)>
Public Property QtyUOM As String
Public Overridable Property Inventory As App_Inventory
Public Overridable Property Location As App_InventoryLocation
End Class
Partial Public Class MyContext
Inherits DbContext
Public Sub New()
MyBase.New("name=MyContext")
End Sub
Public Overridable Property App_Inventory As DbSet(Of App_Inventory)
Public Overridable Property App_InventoryCounts As DbSet(Of App_InventoryCount)
Public Overridable Property App_InventoryLocations As DbSet(Of App_InventoryLocation)
End Class
'InventoryCount'탐색 속성 인'Inventory'와'Location'을주고'Inventory = i'와'Location = l'을 지정하십시오. –
감사합니다. Gert! 나는 그것을 생각한 것 같아. 공식적으로 답변을 드릴 수 없으므로 공식적으로 답글을 남기지 않습니다. –