2016-07-23 1 views
0

私は3つのテーブルを持っています:Inventory、InventoryLocations、InventoryCounts。在庫は商品の説明を保持します。 InventoryLocationsは場所の説明を保持します。 InventoryCountsは、その場所のInventoryID、LocationID、およびアイテム数を持つブリッジテーブルです。最初にコンテキストを保存せずに、複数の新しいアイテムをEntity Frameworkで互いに参照することはできますか?

ファイルをインポートするとき、3つのファイルはすべて新しいファイルにすることができます。コンテキスト内では、在庫アイテムを追加して、DB/EFに在庫ID​​を生成させたいと思います。次に、DBにIDを生成させるロケーション記述を追加したいと思います。次に、インベントリ項目用に生成されたIDとロケーション用に生成されたIDを使用して、InventoryCountsテーブルに新しいレコードを追加します。私はまた、1つのコンテキストと1つの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 
+1

'InventoryCount'ナビゲーションプロパティに' Inventory'と 'Location'を与え、' Inventory = i'と 'Location = l'を割り当ててください。 –

+0

ありがとうGert!私はちょうどそれを考えていたと思う。あなたのコメントに答えとして正式にフラグを立てます(公式にはできません)。 –

答えて

0

が、答えは読むことを追加するためにコードを変更することでした:

Dim cnt As New App_InventoryCount() With {.InventoryID = i.InventoryID, .Qty = 1, .QtyUOM = "Each", .Location = l} 

側の注意点として、私は、また、変更に必要なすべての符号なし整数を整数に変換します。どうやらEntity FrameworkはUIntや大きなUIntを好まないようです。 See post here

関連する問題