0

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
    {

    }
loremIpsum1771
  • 2,497
  • 5
  • 40
  • 87
  • Try the command: Add-Migration IdentityAdded -Context DBContext (I am not sure but may be due to case sensitive) or that the context name you specified is not the same you have in your Startup.cs – Bruno Casarotti May 28 '18 at 21:08
  • @BrunoCasarotti Idk, I copied the name of the dbcontext as is defined: `Add-Migration IdentityAdded -Context DBContext`. Did I need to run the InitialCreate command before I reverse engineered the db? – loremIpsum1771 May 28 '18 at 21:17
  • Yes, I think so. Check this link, seems to be waht you need -> https://msdn.microsoft.com/en-us/library/dn579398(v=vs.113).aspx – Bruno Casarotti May 28 '18 at 21:23
  • I'll check out the second video from the link for migrations with an existing db, but it looks like they are using an old version of EF there. – loremIpsum1771 May 28 '18 at 21:59

2 Answers2

0

I believe that the parameter -Context that you pass into the Add-Migration is case-sensitive.

Also, your DBContext that you use for the AspNet Identity must inherit from IdentityDbContext

Bruno Casarotti
  • 623
  • 8
  • 23
0

It seems like they have similar issue here: Enable-Migrations Exception calling "SetData" with "2" argument(s)

Enable-Migrations installation error

Or maybe you have this issue: https://github.com/aspnet/EntityFramework6/issues/231

hujtomi
  • 1,540
  • 2
  • 17
  • 23