मुझे अपने लिए एक अस्थायी कामकाज मिला है।
हम एक विधि SeedData
कि IApplicationBuilder
फैली तो GetService
विधि के माध्यम से हमारे डाटाबेस संदर्भ वर्ग का एक उदाहरण हो जाता है और डेटा बोने के लिए इसे इस्तेमाल करता है बना सकते हैं।
यहाँ कैसे अपने एक्सटेंशन विधि की तरह लग रहा है:
using Microsoft.AspNet.Builder;
using Microsoft.Extensions.DependencyInjection;
public static class DataSeeder
{
// TODO: Move this code when seed data is implemented in EF 7
/// <summary>
/// This is a workaround for missing seed data functionality in EF 7.0-rc1
/// More info: https://github.com/aspnet/EntityFramework/issues/629
/// </summary>
/// <param name="app">
/// An instance that provides the mechanisms to get instance of the database context.
/// </param>
public static void SeedData(this IApplicationBuilder app)
{
var db = app.ApplicationServices.GetService<ApplicationDbContext>();
// TODO: Add seed logic here
db.SaveChanges();
}
}
यह (Startup.cs
बुलाया फ़ाइल में वेब परियोजना में स्थित) आवेदन Startup
वर्ग के Configure
विधि में app.SeedData();
लाइन डाल का उपयोग करें।
// This method gets called by the runtime.
// Use this method to configure the HTTP request pipeline.
public void Configure(
IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
app.SeedData();
// Other configuration code
}
स्रोत
2015-12-30 19:34:22
आप इस लेख देख सकते हैं एमएसडीएन ब्लॉग में: https://blogs.msdn.microsoft.com/dotnet/2016/09/29/implementing-seeding- कस्टम-conventions-and-interceptors-in-ef-core-1-0/ –