|
using Microsoft.Data.Entity;
using
|
bet | 4/5 | Sana | 25.01.2024 | Hajmi | 0,79 Mb. | | #145486 |
Bog'liq Mavzu Web dasturlash Rejausing Microsoft.Data.Entity;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Linq;
namespace ContosoBooks.Models {
public static class SampleData {
public static void Initialize(IServiceProvider serviceProvider) {
var context = serviceProvider.GetService();
context.Database.Migrate();
if (!context.Book.Any()) {
var austen = context.Author.Add(
new Author { LastName = "Austen", FirstMidName = "Jane" }).Entity;
var dickens = context.Author.Add(
new Author { LastName = "Dickens", FirstMidName = "Charles" }).Entity;
var cervantes = context.Author.Add(
new Author { LastName = "Cervantes", FirstMidName = "Miguel" }).Entity;
context.Book.AddRange(
new Book() {
Title = "Pride and Prejudice",
Year = 1813,
Author = austen,
Price = 9.99M,
Genre = "Comedy of manners"
},
new Book() {
Title = "Northanger Abbey",
Year = 1817,
Author = austen,
Price = 12.95M,
Genre = "Gothic parody"
},
new Book() {
Title = "David Copperfield",
Year = 1850,
Author = dickens,
Price = 15,
Genre = "Bildungsroman"
},
new Book() {
Title = "Don Quixote",
Year = 1617,
Author = cervantes,
Price = 8.95M,
Genre = "Picaresque"
}
);
context.SaveChanges();
}
}
}
}
Siz ushbu ma'lumot sinfini ishlab chiqarish kodiga kiritmaysiz, ammo u bizning stsenariyimiz uchun ishlaydi.
Keyingi, Solution Explorer-da, Startup.cs faylini oching. Configure usuliga quyidagi satrlarni qo'shing:
SampleData.Initialize(app.ApplicationServices);
Shundan so'ng, to'liq Startup.cs fayli quyidagicha bo'ladi:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Data.Entity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ContosoBooks.Models;
using ContosoBooks.Services;
namespace ContosoBooks
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsDevelopment())
{
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets();
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddIdentity()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
|
| |