【问题标题】:Entity Framework creating database but not tables实体框架创建数据库但不创建表
【发布时间】:2019-12-24 07:32:39
【问题描述】:

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

这是我的模型之一:

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

}
   }

这是我的 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)
    {

    }
}
}

这是一个迁移:

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

}

这是我的文件夹结构:

【问题讨论】:

    标签: entity-framework asp.net-core database-migration entity-framework-migrations


    【解决方案1】:

    您只是在没有 getter 和 setter 的情况下公开 DbSet&lt;T&gt;

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

    public DbSet<User> Users { get; set; }
    
    public DbSet<Workout> Workouts { get; set; }
    
    public DbSet<Exercise> Exercises { get; set; }
    

    或者更好的是,使用 fluent-API 并且根本不暴露 DbSet&lt;T&gt;

       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&lt;T&gt;。例如context.Set&lt;User&gt; 检索用户数据库集。

    仅供参考:目前还没有ExerciseSet 的数据库集,它是@​​987654330@ 的子集。

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

    【讨论】:

    • 谢谢。因为那个疏忽,我浪费了一整晚。
    猜你喜欢
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多