我想在我的网络项目中播种我的数据库并阅读我能发现的任何内容似乎是一项艰巨的任务。我想你可以得到DbContext
然后我就可以将它添加到迁移文件中的Up()
。这就是我所拥有的
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Migrations;
using student.Data;
using student.Models;
using System.Linq;
namespace student.Migrations
{
public partial class INITAL_seed : Migration
{
private readonly ApplicationDbContext _context;
public INITAL_seed()
{}
public INITAL_seed(ApplicationDbContext context)
{
_context = context;
}
protected override void Up(MigrationBuilder migrationBuilder)
{
// Look for any students.
if (_context.invoice_state.Any())
{
return; // DB has been seeded
}
var invoice_states = new invoice_state[]
{
new invoice_state{title="Approval Denied",alias="approval_false"},
new invoice_state{title="Needing Finish",alias="start_finish"},
new invoice_state{title="Needing Approval",alias="approve_finish"},
new invoice_state{title="Needing Send",alias="send_finish"},
new invoice_state{title="Locked",alias="lock_finish"}
};
foreach (invoice_state IN in invoice_states)
{
_context.invoice_state.Add(IN);
}
_context.SaveChanges();
}
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}
麻烦的是它需要无参数构造。我需要能够解决这个问题,我相信那会有用。所以,如果我更正
public INITAL_seed()
{}
public INITAL_seed(ApplicationDbContext context)
{
_context = context;
}
类似
public INITAL_seed()
{
_context = new ApplicationDbContext();
}
我会被设定。这不起作用,因为我需要获得选项,因为这是DbContext
类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using student.Models;
using System.ComponentModel.DataAnnotations.Schema;
using student.Services;
using Microsoft.AspNetCore.Identity;
namespace student.Data
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
public DbSet<invoice_state> invoice_state { get; set; }
}
}
我的问题是如何通过大量的新线路来实现这一目标。