首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >第一次运行数据库迁移时出错

第一次运行数据库迁移时出错
EN

Stack Overflow用户
提问于 2018-05-29 04:20:28
回答 2查看 814关注 0票数 0

我有一个ASP核心应用程序,使用现有的表从.Net服务器数据库。由于这些表在我创建应用程序之前就已经存在,为了在应用程序中构建它们的模型,我使用以下命令对这些表进行了逆向工程:

代码语言:javascript
复制
Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

使用这个命令成功地创建了模型,到目前为止,我已经能够根据需要从数据库表中进行查询。现在,我正在为应用程序的用户注册系统工作,并已添加了必要的app .Net实体框架核心身份服务,作为过程的一部分,所需的应用程序用户模型需要迁移到数据库。

我首先尝试(在包管理器控制台中)运行命令add-migration IdentityAdded -context DBContext,但出现错误:Add-Migration : A parameter cannot be found that matches parameter name 'context'。在此之后,我认为因为我还没有运行迁移,所以我需要运行命令Add-Migration InitialCreate,但在运行该命令后,出现以下错误:

代码语言:javascript
复制
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.

由于我最初对数据库表进行了反向工程,是否可以不再运行迁移?是什么导致了这个问题?

以下是启动文件以供参考:

startup.cs

代码语言:javascript
复制
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}");
        }

            );

编辑

应用程序用户类:

代码语言:javascript
复制
 public class ApplicationUser : IdentityUser
    {

    }
EN

回答 2

Stack Overflow用户

发布于 2018-05-29 05:17:31

我相信您传递给Add-Migration的参数-Context是区分大小写的。

此外,用于AspNet标识的DBContext必须继承自IdentityDbContext

票数 0
EN

Stack Overflow用户

发布于 2018-05-29 05:58:33

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50573011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档