I have an ASP .Net core app which uses existing tables from a sql server database. As the tables had preexisted before I created the app, in order to build models of them within the app, I reverse engineered the tables using the command:
Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
Using this command successfully created the models and I have been able to query from the database tables as need so far. Right now, I'm working on the User Registration system for the app and have added the necessary ASP .Net entity framework core identity service and as a part of the process, the required Application user model needs to be migrated to the database.
I tried first running (in the package manager console) the command add-migration IdentityAdded -context DBContext
and there was an error saying: Add-Migration : A parameter cannot be found that matches parameter name 'context'
. After this, I thought that since I hadn't yet run a migration, I needed to run the command Add-Migration InitialCreate
, but after running the command, there is the error:
Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject' in assembly
'Microsoft.VisualStudio.ProjectSystem.VS.Implementation, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d2940a' is not marked as serializable.
Can migrations no longer be run since I reverse engineered the database tables initially? What could be causing the problem?
Here is the startup file for reference:
startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//using( var context = new ApplicationDbContext())
//{
// context.Database.EnsureCreated();
//}
services.AddDbContext<DBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Cn")));
services.AddMvc();
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<DBContext>()
.AddDefaultTokenProviders();
services.Configure<IdentityOptions>(options =>
{
// Password settings
options.Password.RequireDigit = true;
options.Password.RequiredLength = 8;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = true;
options.Password.RequireLowercase = false;
options.Password.RequiredUniqueChars = 6;
// Lockout settings
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
options.Lockout.MaxFailedAccessAttempts = 10;
options.Lockout.AllowedForNewUsers = true;
// User settings
options.User.RequireUniqueEmail = true;
});
services.ConfigureApplicationCookie(options =>
{
// Cookie settings
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
// If the LoginPath isn't set, ASP.NET Core defaults
// the path to /Account/Login.
options.LoginPath = "/Account/Login";
// If the AccessDeniedPath isn't set, ASP.NET Core defaults
// the path to /Account/AccessDenied.
options.AccessDeniedPath = "/Account/AccessDenied";
options.SlidingExpiration = true;
});
services.AddPaging();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
//app.UseIdentity(); //will be deprecated
AuthAppBuilderExtensions.UseAuthentication(app);
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=resortDeals}/{action=Index}/{id?}");
//routes.MapRoute(
// name: "DetailsRoute",
// template: "{controller=resortDeals}/{action=Details}/{id}");
}
);
Edit
Application user class:
public class ApplicationUser : IdentityUser
{
}