I have the following code to seed my UserRoles table:
if (context.UserRoles.Any())
{
return;
}
else
//add user roles
{
var adminPersonID = context.Users.Where(u => u.UserName.ToLower().Contains("admin")).Select(u => u.Id).FirstOrDefault();
var publisherPersonID = context.Users.Where(u => u.UserName.ToLower().Contains("publisher")).Select(u => u.Id).FirstOrDefault();
var adminRoleID = context.Roles.Where(u => u.Name.ToLower().Equals("administrator")).Select(u => u.Id).FirstOrDefault();
var publisherRoleID = context.Roles.Where(u => u.Name.ToLower().Equals("publisher")).Select(u => u.Id).FirstOrDefault();
var userRoles = new List<UserRole>()
{
new UserRole { UserID = adminPersonID, RoleID = adminRoleID },
new UserRole { UserID = publisherPersonID, RoleID = publisherRoleID }
};
context.AddRange(userRoles);
context.SaveChanges();
}Everything's fine until it reaches the line "context.AddRange(userRoles);"--that is, the right values are picked up for the "new UserRole" lines, etc. The problem is that it returns the following error:
System.InvalidOperationException was unhandled by user code
HResult=-2146233079
Message=The entity type 'UserRole' was not found. Ensure that the entity type has been added to the model.
Source=Microsoft.EntityFrameworkCore
StackTrace:
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.GetOrCreateEntry(Object entity)
at Microsoft.EntityFrameworkCore.DbContext.SetEntityStates(IEnumerable`1 entities, EntityState entityState)
at RIMS.Infrastructure.DataSeeder.EnsureDataSeeded(RimsContext context) in C:\Users\bnilsen\Source\Repos\rims\RIMS\src\RIMS\Infrastructure\DataSeeder.cs:line 214
at RIMS.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) in C:\Users\bnilsen\Source\Repos\rims\RIMS\src\RIMS\Startup.cs:line 127
My question is: How do I find which model the above error is referring to? "UserRole" is found in the User model, the User viewmodel, and in the DataContext. Not sure where else to look.