【问题标题】:EF Core migrations not working in in ASP.NET Core web project. Empty Up/DownEF Core 迁移在 ASP.NET Core Web 项目中不起作用。空上/下
【发布时间】:2016-09-19 12:36:37
【问题描述】:

我刚开始使用 EF Core 构建一个新的 ASP.Net Core 网站。我使用带有用户身份验证的 Visual Studio 中的普通模板创建了模板。很基础。包括 ApplicationDbContext 和迁移,我成功地使用身份表更新了数据库。 然后我添加自己的类并将它们作为 DbSet 添加到 ApplicationDbContext。尝试使用 Add-Migration 但 Up 和 Down 方法是空的。我尝试了不同的解决方案,但其中大多数建议将 dbset 添加到 db 上下文类中......我已经完成了。 我在这里没有看到什么?

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?}");
        });
    }
}

【问题讨论】:

    标签: asp.net entity-framework asp.net-core code-first entity-framework-migrations


    【解决方案1】:

    好的,所以我解决了这个问题......有点。作为一次精神错乱检查,我创建了一个新的 Web 项目并一次添加一个文件。工作!我想我通过移动 ApplicationDbContext 类和带有迁移的 Data 文件夹搞砸了另一个 Web 项目......所以我就让它保持吧:)

    【讨论】:

      猜你喜欢
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-30
      • 2021-11-23
      • 2016-11-06
      • 1970-01-01
      • 2022-11-11
      相关资源
      最近更新 更多