我有一个ASP .Net核心应用程序,它使用sql server数据库中的现有表。由于这些表在我创建应用程序之前已经存在,为了在应用程序中构建它们的模型,我使用命令对表进行了反向设计:
Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
使用此命令成功创建了模型,到目前为止我已经能够根据需要从数据库表中进行查询。现在,我正在为应用程序的用户注册系统工作并添加了必要的ASP .Net实体框架核心身份服务,并且作为流程的一部分,需要将所需的应用程序用户模型迁移到数据库。
我尝试首先运行(在包管理器控制台中)命令add-migration IdentityAdded -context DBContext
并且出现错误:Add-Migration : A parameter cannot be found that matches parameter name 'context'
。在此之后,我认为既然我还没有运行迁移,我需要运行命令Add-Migration InitialCreate
,但在运行命令后,出现错误:
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
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}");
}
);
应用用户类:
public class ApplicationUser : IdentityUser
{
}
答案 0 :(得分:0)
我认为您传递给-Context
的参数Add-Migration
区分大小写。
此外,您用于AspNet标识的DBContext
必须继承自IdentityDbContext
答案 1 :(得分:0)