2016-11-20 66 views
1
不工作

在实体框架6 dosent播种我的实现代码似乎与核心的EntityFramework兼容播种数据库imlementation在EntityFrameworkcore

这里是我的代码

public class CustomerOrderSeedData : DropCreateDatabaseIfModelChanges<CustomerOrderEntities> 
    { 
     protected override void Seed(CustomerOrderEntities context) 
     { 

      GetOrderDetails().ForEach(od => context.OrdersDetails.Add(od)); 

      context.Commit(); 
     } 

private static List<OrdersDetails> GetOrderDetails() 
     { 
      return new List<OrdersDetails> 
      { 
       new OrdersDetails { 
        OrderId = 1, 
        ProductId = 1, 
        Quantity = 10, 
        UnitPrice = 12, 
        Discount = 3 

       }, 
       new OrdersDetails { 
        OrderId = 1, 
        ProductId = 2, 
        Quantity = 3, 
        UnitPrice = 4, 
        Discount = 2 
       } 
     } 
} 

EntityFrameworkCore似乎并不喜欢DropCreateDatabaseIfModelChanges关键字。有人告诉我一个如何使用EntityFrameworkcore完成播种的例子。

回答

0

是的。您不能在EF内核上使用EF 6.X实现.B'cos EF核心具有不同的API。

您可以在Startup.Configure()的服务范围内运行种子代码,如下所示。

using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope()) 
{ 
     var context = serviceScope.ServiceProvider.GetService<MyContext>();  
     context.Database.Migrate(); 
     context.EnsureSeedData(); 
} 

这里,你可以看到上面的代码片段的完整实现的链接(参见下播种部分)。

Implementing Seeding EF Core 1.0