Entity framework 实体框架创建数据库而不是表
当我运行迁移时,数据库正在创建,但没有一个表在创建。我不知道我做错了什么,因为前几天我做了同样的事情,没有任何问题。初始迁移运行并创建了数据库,但没有一个表。我尝试过删除数据库和迁移,然后重新执行整个过程,但没有成功。下面是我的文件夹结构的一些代码和图片。希望有人能指出我做错了什么 以下是我的一个模型:Entity framework 实体框架创建数据库而不是表,entity-framework,asp.net-core,database-migration,entity-framework-migrations,Entity Framework,Asp.net Core,Database Migration,Entity Framework Migrations,当我运行迁移时,数据库正在创建,但没有一个表在创建。我不知道我做错了什么,因为前几天我做了同样的事情,没有任何问题。初始迁移运行并创建了数据库,但没有一个表。我尝试过删除数据库和迁移,然后重新执行整个过程,但没有成功。下面是我的文件夹结构的一些代码和图片。希望有人能指出我做错了什么 以下是我的一个模型: using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks;
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; }
}
}
下面是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?}");
});
}
}
u跟踪器
{
公营创业
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
配置(选项=>
{
//此lambda确定给定请求是否需要非必要cookie的用户同意。
options.checkApprovered=context=>true;
options.MinimumSameSitePolicy=SameSiteMode.None;
});
services.AddDbContext您只是在没有getter和setter的情况下公开了DbSet
将数据库集更改为:
public DbSet<User> Users { get; set; }
public DbSet<Workout> Workouts { get; set; }
public DbSet<Exercise> Exercises { get; set; }
公共数据库集用户{get;set;}
公共数据库集训练{get;set;}
公共数据库集练习{get;set;}
或者更好的是,使用fluent API,并且根本不公开DbSet
模型创建时受保护的覆盖无效(ModelBuilder)
{
//您可以在此处定义表名、键和索引
//还有“IEntityTypeConfiguration”,它更好,因为它保持了DbContext的干净
// https://codeburst.io/ientitytypeconfiguration-t-in-entityframework-core-3fe7abc5ee7a
builder.Entity();
builder.Entity();
builder.Entity();
}
然后,在注入您的ApplicationDbContext
时,您可以使用通用方法context.Set
。例如context.Set
来检索用户数据库集
仅供参考:ExerciseSet
是Exercise
的子集,目前也没有db设置
for entity框架非常好,我建议您熟悉它们。您只是在没有getter和setter的情况下公开了DbSet
将数据库集更改为:
public DbSet<User> Users { get; set; }
public DbSet<Workout> Workouts { get; set; }
public DbSet<Exercise> Exercises { get; set; }
公共数据库集用户{get;set;}
公共数据库集训练{get;set;}
公共数据库集练习{get;set;}
或者更好的是,使用fluent API,并且根本不公开DbSet
模型创建时受保护的覆盖无效(ModelBuilder)
{
//您可以在此处定义表名、键和索引
//还有“IEntityTypeConfiguration”,它更好,因为它保持了DbContext的干净
// https://codeburst.io/ientitytypeconfiguration-t-in-entityframework-core-3fe7abc5ee7a
builder.Entity();
builder.Entity();
builder.Entity();
}
然后,在注入您的ApplicationDbContext
时,您可以使用通用方法context.Set
。例如context.Set
来检索用户数据库集
仅供参考:ExerciseSet
是Exercise
的子集,目前也没有db设置
for entity框架非常好,我建议您熟悉它们。谢谢。我因为疏忽浪费了一个晚上。谢谢。我因为疏忽浪费了一个晚上。
public DbSet<User> Users { get; set; }
public DbSet<Workout> Workouts { get; set; }
public DbSet<Exercise> Exercises { get; set; }