1

When I run the migration, the database is creating but none of the tables are. I have no clue what I'm doing wrong as I did the same thing the other day with no issue. The initial migration ran and created the database but, none of the tables. I've tried deleting the db and migrations and doing the whole process over again with no luck. Below is some code and a picture of my folder structure. Hopefully someone can point out what I'm doing wrong.

Here is one of my models:

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; }

}
   }

Here is my dbcontext:

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)
    {

    }
}
}

Here is a migration:

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

    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {

    }
}
}

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

}

And here is my folder structure:

enter image description here

1 Answer 1

3

You are just exposing the DbSet<T> without a getter and setter.

Either change your db-sets to this:

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

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

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

Or even better, use the fluent-API and don't expose DbSet<T> at all.

   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>();
   }

Then when injecting your ApplicationDbContext you can use the generic-method context.Set<T>. For instance context.Set<User> to retrieve the users db-set.

FYI: There is also currently no db-set for ExerciseSet which is a subset of Exercise.

The docs for entity-framework are very good, I'd recommend to get familiar with them.

1
  • Thank you. I wasted a whole night because of that oversight.
    – QHafeez
    Commented Aug 19, 2019 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.