EF Core 迁移在 ASP.NET 核心 Web 项目中不起作用。空 Up/Down
EF Core migrations not working in in ASP.NET Core web project. Empty Up/Down
我刚开始使用 EF Core 构建一个新的 ASP.Net Core 网站。我使用 Visual Studio 中带有用户身份验证的普通模板创建了模板。非常基本。包含 ApplicationDbContext 和迁移,我成功地使用身份表更新了数据库。
然后我添加我自己的 classes 并将它们作为 DbSet 添加到 ApplicationDbContext。尝试使用 Add-Migration,但 Up 和 Down 方法为空。我尝试了不同的解决方案,但其中大多数建议将 dbset 添加到数据库上下文 class... 我已经完成了。
我在这里没有看到什么?
ApplicationDbContext
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Customer> Customers { get; set; }
public DbSet<UserManual> UserManuals { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<UserManualCustomer>()
.HasKey(t => new { t.CustomerId, t.UserManualId});
builder.Entity<UserManualCustomer>()
.HasOne(pt => pt.Customer)
.WithMany(p => p.UserManualCustomer)
.HasForeignKey(pt => pt.CustomerId);
builder.Entity<UserManualCustomer>()
.HasOne(pt => pt.UserManual)
.WithMany(p => p.UserManualCustomer)
.HasForeignKey(pt => pt.UserManualId);
base.OnModelCreating(builder);
}
}
Startup.cs
public class Startup
{
public const string ConnectionString = @"Server=Server=(localdb)\ProjectsV13;Database=FSCIDb;Trusted_Connection=true;MultipleActiveResultSets=true";
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsDevelopment())
{
builder.AddUserSecrets();
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
services.AddScoped<IRepository, Repository>();
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseIdentity();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
好的,所以我解决了问题...有点。作为一项疯狂检查,我创建了一个新的 Web 项目并一次添加一个文件。成功了!我想我通过移动 ApplicationDbContext classe 和带有迁移的 Data 文件夹搞砸了另一个 web 项目...所以我将保留它:)
我刚开始使用 EF Core 构建一个新的 ASP.Net Core 网站。我使用 Visual Studio 中带有用户身份验证的普通模板创建了模板。非常基本。包含 ApplicationDbContext 和迁移,我成功地使用身份表更新了数据库。 然后我添加我自己的 classes 并将它们作为 DbSet 添加到 ApplicationDbContext。尝试使用 Add-Migration,但 Up 和 Down 方法为空。我尝试了不同的解决方案,但其中大多数建议将 dbset 添加到数据库上下文 class... 我已经完成了。 我在这里没有看到什么?
ApplicationDbContext
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Customer> Customers { get; set; }
public DbSet<UserManual> UserManuals { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<UserManualCustomer>()
.HasKey(t => new { t.CustomerId, t.UserManualId});
builder.Entity<UserManualCustomer>()
.HasOne(pt => pt.Customer)
.WithMany(p => p.UserManualCustomer)
.HasForeignKey(pt => pt.CustomerId);
builder.Entity<UserManualCustomer>()
.HasOne(pt => pt.UserManual)
.WithMany(p => p.UserManualCustomer)
.HasForeignKey(pt => pt.UserManualId);
base.OnModelCreating(builder);
}
}
Startup.cs
public class Startup
{
public const string ConnectionString = @"Server=Server=(localdb)\ProjectsV13;Database=FSCIDb;Trusted_Connection=true;MultipleActiveResultSets=true";
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsDevelopment())
{
builder.AddUserSecrets();
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
services.AddScoped<IRepository, Repository>();
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseIdentity();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
好的,所以我解决了问题...有点。作为一项疯狂检查,我创建了一个新的 Web 项目并一次添加一个文件。成功了!我想我通过移动 ApplicationDbContext classe 和带有迁移的 Data 文件夹搞砸了另一个 web 项目...所以我将保留它:)