Entity Framework 创建数据库但不创建表

Entity Framework creating database but not tables

当我 运行 迁移时,数据库正在创建,但表的 none 正在创建。我不知道我做错了什么,因为前几天我做了同样的事情,没有任何问题。初始迁移 运行 并创建了数据库,但是 none 的表。我试过删除数据库和迁移并重新完成整个过程但没有运气。下面是一些代码和我的文件夹结构的图片。希望有人能指出我做错了什么。

这是我的模型之一:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Workout_Tracker.Models
{
public class Exercise
{
    public int ID { get; set; }

    public int Weight { get; set; }

    public string Name { get; set; }

    public int WorkoutID { get; set; }
    public Workout Workout { get; set; }

    public IList<ExerciseSet> Sets { get; set; }

}
   }

这是我的数据库上下文:

namespace Workout_Tracker.Data
{
    public class ApplicationDbContext : DbContext
   {

    public DbSet<User> Users;

    public DbSet<Workout> Workouts;

    public DbSet<Exercise> Exercises;

    public DbSet<ExerciseSet> Exercise_Sets;
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {

    }
}
}

这是一个迁移:

namespace Workout_Tracker.Migrations
{
    public partial class first : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {

    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {

    }
}
}

这里是startup.cs:

namespace Workout_Tracker

{
    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)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));


        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

    // 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.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();

        app.UseAuthentication();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

}

这是我的文件夹结构:

你只是暴露了 DbSet<T> 而没有 getter 和 setter。

要么将您的数据库集更改为:

public DbSet<User> Users { get; set; }

public DbSet<Workout> Workouts { get; set; }

public DbSet<Exercise> Exercises { get; set; }

或者更好,使用 fluent-API 并且根本不暴露 DbSet<T>

   protected override void OnModelCreating(ModelBuilder builder)
   {
      // you can define table names, keys and indices here
      // there is also `IEntityTypeConfiguration<T>` which is much better since it keeps the DbContext clean
      // https://codeburst.io/ientitytypeconfiguration-t-in-entityframework-core-3fe7abc5ee7a
      builder.Entity<User>();
      builder.Entity<Workout>();
      builder.Entity<Exercise>();
   }

然后在注入您的 ApplicationDbContext 时,您可以使用通用方法 context.Set<T>。例如 context.Set<User> 检索用户数据库集。

仅供参考:目前还没有 ExerciseSet 的数据库集,它是 Exercise.

的子集

entity-framework的docs非常好,建议熟悉一下。