Vb.net 为什么我的实体框架迁移种子在更新数据库上运行,但没有放入任何数据?
运行update datebase时,我得到以下响应: PM>更新数据库-详细 使用启动项目“BestOrders” 使用NuGet项目“BestOrders” 指定'-Verbose'标志以查看应用于目标数据库的SQL语句 目标数据库是:“Orders.sdf”(数据源:Orders.sdf,提供程序:System.Data.SqlServerCe.4.0,源代码:Configuration) 没有挂起的基于代码的迁移 运行种子法 PM> 现在seed方法(下面给出了完整的代码)显然可以运行了,因为它花费了不少时间,但数据库中没有数据。我错过了什么Vb.net 为什么我的实体框架迁移种子在更新数据库上运行,但没有放入任何数据?,vb.net,entity-framework,ef-code-first,entity-framework-migrations,Vb.net,Entity Framework,Ef Code First,Entity Framework Migrations,运行update datebase时,我得到以下响应: PM>更新数据库-详细 使用启动项目“BestOrders” 使用NuGet项目“BestOrders” 指定'-Verbose'标志以查看应用于目标数据库的SQL语句 目标数据库是:“Orders.sdf”(数据源:Orders.sdf,提供程序:System.Data.SqlServerCe.4.0,源代码:Configuration) 没有挂起的基于代码的迁移 运行种子法 PM> 现在seed方法(下面给出了完整的代码)显然可以运行了
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
导入系统
导入System.Data.Entity
导入System.Data.Entity.Migrations
导入系统
命名空间迁移
好友不可继承类配置
继承(OrdersContext的)DBMigOptionsConfiguration
公共分新()
AutomaticMiggerationsEnabled=真
端接头
受保护的覆盖子种子(作为OrdersContext的上下文)
常量ItemCount为整数=100
常量供应商计数为整数=5
Const PartsRequestCount为整数=25
Const locationscont为整数=3
常量MaxQuantity为整数=5
常数Seed为整数=10
Const DeliveryChance作为小数=0.5
Dim Delivery()作为整数={10,90}
Dim dischance(,)作为十进制={{0.05,0.07,0.08},
{0.025, 0.035, 0.05}}
Dim PriceRange()作为十进制={50,5000}单位为美分!
十进制尺寸差异=10'的百分比
Dim多供应商机会=0.66
Dim rnd=新随机(种子)
对于i=1的供应商计数
Dim sup=具有{.ID=i的新供应商,
.Name=String.Format(“供应商{0:D3}”,i),
.Delivery=IIf(rnd.NextDouble我解决了问题。我忘了保存修改过的上下文。您是否尝试保存更改?例如c ontext.SaveChanges()在你的方法结束时。@yauheni_selivonchyk我已经完成了,但在这个版本中没有。我在某处读到它在基成员上被调用。尽管如此,我将把它放回去看看会发生什么。明天之前没有可用的计算机though@yauheni_selivonchyk原来我是个白痴——谢谢你礼貌地指出这一点。