为什么我的Entity Framework迁移种子在update-database上运行但没有放入任何数据?

时间:2013-01-31 04:39:25

标签: vb.net entity-framework ef-code-first ef-migrations

当我运行update-datebase时,我得到以下响应:

  

PM> update-database -verbose

     

使用StartUp项目'BestOrders'。

     

使用NuGet项目'BestOrders'。

     

指定'-Verbose'标志以查看应用于目标数据库的SQL语句。

     

目标数据库是:'Orders.sdf'(DataSource:Orders.sdf,Provider:System.Data.SqlServerCe.4.0,Origin:Configuration)。

     

没有待处理的基于代码的迁移。

     

运行种子方法。

     

PM>

现在种子方法(下面给出的完整代码)清楚地运行,因为它花费了不可忽视的时间,但数据库中没有数据。我错过了什么?

Imports System
Imports System.Data.Entity
Imports System.Data.Entity.Migrations
Imports System.Linq

Namespace Migrations

    Friend NotInheritable Class Configuration 
        Inherits DbMigrationsConfiguration(Of OrdersContext)

        Public Sub New()
            AutomaticMigrationsEnabled = True
        End Sub

        Protected Overrides Sub Seed(context As OrdersContext)
            Const ItemCount As Integer = 100
            Const SupplierCount As Integer = 5
            Const PartsRequestCount As Integer = 25
            Const LocationsCount As Integer = 3
            Const MaxQuantity As Integer = 5
            Const Seed As Integer = 10
            Const DeliveryChance As Decimal = 0.5
            Dim Delivery() As Integer = {10, 90}
            Dim DiscChance(,) As Decimal = {{0.05, 0.07, 0.08},
                                           {0.025, 0.035, 0.05}}
            Dim PriceRange() As Decimal = {50, 5000} 'In cents!
            Dim Variance As Decimal = 10 'In percent
            Dim MultiSupplierChance = 0.66

            Dim rnd = New Random(Seed)

            For i = 1 To SupplierCount
                Dim sup = New Supplier With {.ID = i,
                                             .Name = String.Format("Supplier{0:D3}", i),
                                             .Delivery = IIf(rnd.NextDouble <= DeliveryChance,
                                                             rnd.Next(Delivery(0) / 10, Delivery(1) / 10) * 10,
                                                             0),
                                             .Discount = 0}

                Dim r = rnd.NextDouble
                For k = DiscChance.GetLowerBound(1) To DiscChance.GetUpperBound(1)
                    If r <= DiscChance(0, k) Then
                        sup.Discount = DiscChance(1, k)
                        Exit For
                    End If
                Next

                context.Suppliers.AddOrUpdate(sup)
            Next

            Dim j As Integer = 1
            For i = 1 To ItemCount
                Dim itm = New Item With {.ID = i,
                                         .Number = String.Format("Item{0:D4}", i),
                                         .SupplierItems = New List(Of SupplierItem)}
                Debug.Print("{0}: {1}", itm.ID, itm.Number)
                context.Items.AddOrUpdate(itm)
                Dim FirstSup = New SupplierItem With {.ID = j,
                                                      .Supplier = context.Suppliers.Find(rnd.Next(1, SupplierCount)),
                                                      .Price = rnd.Next(PriceRange(0), PriceRange(1)) * 100}

                j += 1
                itm.SupplierItems.Add(FirstSup)
                While rnd.NextDouble <= MultiSupplierChance And itm.SupplierItems.Count < SupplierCount
                    Dim NextSup = New SupplierItem With {.ID = j,
                                                         .Supplier = context.Suppliers.Find(rnd.Next(1, SupplierCount)),
                                                         .Price = FirstSup.Price + rnd.Next(-Variance, Variance) * 100}
                    If itm.SupplierItems.First(Function(s) s.Supplier.ID = NextSup.Supplier.ID) Is Nothing Then
                        j += 1
                        itm.SupplierItems.Add(NextSup)
                    End If
                End While

            Next

            For i = 1 To LocationsCount
                Dim loc = New Location With {.ID = i,
                                             .Name = String.Format("Location{0:D2}", i)}
                context.Locations.AddOrUpdate(loc)
            Next

            For i = 1 To PartsRequestCount
                Dim pr = New PartsRequest With {.ID = i,
                                                .Item = context.Items.Find(rnd.Next(1, ItemCount)),
                                                .Location = context.Locations.Find(rnd.Next(1, LocationsCount)),
                                                .Quantity = rnd.Next(1, MaxQuantity),
                                                .RequestTime = Now().AddMinutes(-rnd.Next(0, 10080))}
                context.PartsRequests.AddOrUpdate(pr)
            Next


            MyBase.Seed(context)
        End Sub

    End Class

End Namespace

1 个答案:

答案 0 :(得分:0)

我弄明白了这个问题。我忘了保存修改后的上下文。