Reputation: 91
My command line says there is no reference set for my GetProvinces
methods list of provinces... but I'm 99.99999% sure it is. Does anyone know whats going on??
Console error message:
dotnet-ef migrations add InitialCreate -o Data/Migrations
Build started...
Build succeeded.
The Entity Framework tools version '5.0.10' is older than that of the runtime '5.0.11'. Update the tools for the latest features and bug fixes.
System.NullReferenceException: Object reference not set to an instance of an object.at MyApp.Data.SampleData.GetProvinces() in C:\Users\Owner\Downloads\COMP 3973\In progress\COMP3973-lab4_A01045801-Michael-Green\MyApp\Data\SampleData.cs:line 10
at MyApp.Data.ApplicationDbContext.OnModelCreating(ModelBuilder modelBuilder) in C:\Users\Owner\Downloads\COMP 3973\In progress\COMP3973-lab4_A01045801-Michael-Green\MyApp\Data\ApplicationDbContext.cs:line 22
at Microsoft.EntityFrameworkCore.Infrastructure.ModelCustomizer.Customize(ModelBuilder modelBuilder, DbContext context)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, ModelDependencies modelDependencies)
GetProvinces()
method and class basically:
using System.Collections.Generic;
using MyApp.Data;
namespace MyApp.Data
{
public class SampleData
{
// Method for dummy provinces
public static List<Province> GetProvinces()
{
List<Province> provinces = new List<Province>(){
new Province() {
ProvinceCode = "BC",
ProvinceName = "British Columbia",
Cities = {"Vancouver", "Victoria", "Port Coquitlam"}
},
new Province(){
ProvinceCode = "AB",
ProvinceName = "Alberta",
Cities = {"Calgary", "Edmonton", "Banff"}
},
new Province(){
ProvinceCode = "SK",
ProvinceName = "Saskatchewan",
Cities = {"Moose Jaw", "Regina", "Saskatoon"}
},
};
return provinces;
}
}
}
ApplicationDbContext
model creating:
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Province>().HasData(SampleData.GetProvinces());
modelBuilder.Entity<City>().HasData(SampleData.GetCities());
}
public DbSet<Province> Provinces { get; set; }
public DbSet<City> Cities { get; set; }
}
Province model class:
public class Province
{
[Key]
[Display (Name="Province Code")]
[MaxLength(2)]
public string ProvinceCode { get; set; }
[Display (Name="Province Name")]
[MaxLength(30)]
public string ProvinceName { get; set; }
[Display (Name="Cities")]
[MaxLength(50)]
public List<string> Cities { get; set; }
}
I did an extremely similar app to this, with Teams, and Players; and it generated my migrations fine... So I'm not sure what has happened here >:/ Looked at 3 other "object not set to an instance of an object" questions on here and none of them where directly applicable.
Any help would be much appreciated :D
Upvotes: 1
Views: 246
Reputation: 101443
The reason is this syntax:
Cities = {"Vancouver", "Victoria", "Port Coquitlam"}
What it does is it adds specified items to the Cities collection, but it does NOT create the collection. This syntax is mostly intended to fill readonly collection properties. You do not initialize Cities in constructor of Province, so it's null and you have NullReferenceException when trying to add items to null collection. Instead either initialize Cities in constructor of Province or use another syntax:
Cities = new List<string>() {"Vancouver", "Victoria", "Port Coquitlam"}
This one both initializes collection and adds items to it.
Upvotes: 3