ApplicationDbContext 对象引用未设置为对象的实例
ApplicationDbContext Object reference not set to an instance of an object
我正在尝试使用 ASP.Net Core MVC 应用程序将数据播种到我的 Sqlite 数据库中。一切正常,直到命令:
context.SaveChanges()
当它被触发时,一个 "Object reference not set to an instance of an object" 被抛出,尽管事先已经毫无问题地添加到上下文中。这是可疑代码:
public class Seeder
{
private ApplicationDbContext context;
public Seeder(ApplicationDbContext _context)
{
context = _context;
}
//populate the database with the static items
public void SeedData()
{
//jump out if data already exists
if(context.HomeTypes.Count() != 0)
{
return;
}
//build the home types
HomeType basic = new HomeType
{
home_type_id = 0,
name = "Town House",
capacity = 2,
home_image_src = "changeThisLater"
};
context.HomeTypes.Add(basic);
context.SaveChanges();
}
}
这里是调用播种器 class 的地方:
public static void Main(string[] args)
{
var host = BuildWebHost(args);
//seed the database when the app starts up
using(var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<ApplicationDbContext>();
Seeder seeder = new Seeder(context);
seeder.SeedData();
}catch(Exception e)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(e, "An error occured while seeding the db");
}
}
//run
host.Run();
}
这是我的错误消息的照片:
已解决!
问题出在我的 ID 上。在这个播种机中,我将 home_type_id 的值设置为 0。EF Core 自动生成 ID,因此这导致了一个问题,并将我的 ID 添加为 -2175267(或类似的疯狂内容)。
修复只是添加:
//prevent auto generation of the id feilds
builder.Entity<Role>().Property(m => m.role_id).ValueGeneratedNever();
builder.Entity<HomeType>().Property(m => m.home_type_id).ValueGeneratedNever();
在 OnModelCreating 函数中添加到我的 ApplicationDbContext,它现在可以工作 groovy
我正在尝试使用 ASP.Net Core MVC 应用程序将数据播种到我的 Sqlite 数据库中。一切正常,直到命令:
context.SaveChanges()
当它被触发时,一个 "Object reference not set to an instance of an object" 被抛出,尽管事先已经毫无问题地添加到上下文中。这是可疑代码:
public class Seeder
{
private ApplicationDbContext context;
public Seeder(ApplicationDbContext _context)
{
context = _context;
}
//populate the database with the static items
public void SeedData()
{
//jump out if data already exists
if(context.HomeTypes.Count() != 0)
{
return;
}
//build the home types
HomeType basic = new HomeType
{
home_type_id = 0,
name = "Town House",
capacity = 2,
home_image_src = "changeThisLater"
};
context.HomeTypes.Add(basic);
context.SaveChanges();
}
}
这里是调用播种器 class 的地方:
public static void Main(string[] args)
{
var host = BuildWebHost(args);
//seed the database when the app starts up
using(var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var context = services.GetRequiredService<ApplicationDbContext>();
Seeder seeder = new Seeder(context);
seeder.SeedData();
}catch(Exception e)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogError(e, "An error occured while seeding the db");
}
}
//run
host.Run();
}
这是我的错误消息的照片:
已解决!
问题出在我的 ID 上。在这个播种机中,我将 home_type_id 的值设置为 0。EF Core 自动生成 ID,因此这导致了一个问题,并将我的 ID 添加为 -2175267(或类似的疯狂内容)。
修复只是添加:
//prevent auto generation of the id feilds
builder.Entity<Role>().Property(m => m.role_id).ValueGeneratedNever();
builder.Entity<HomeType>().Property(m => m.home_type_id).ValueGeneratedNever();
在 OnModelCreating 函数中添加到我的 ApplicationDbContext,它现在可以工作 groovy