我有以下几种模型:
用户:
public class User
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
public string SocialId { get; set; }
public string AccessToken { get; set; }
public string RefreshToken { get; set; }
public DateTime ExpirationAccess { get; set; }
public DateTime ExpirationRefresh { get; set; }
public string AuthType { get; set; }
public decimal Money { get; set; }
public User()
{
CreatedAt = DateTime.UtcNow;
UpdatedAt = DateTime.UtcNow;
AccessToken = Guid.NewGuid().ToString();
RefreshToken = Guid.NewGuid().ToString();
ExpirationAccess = DateTime.UtcNow.AddDays(1);
ExpirationRefresh = DateTime.UtcNow.AddMonths(1);
}
}
食谱:
public class Recipe
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public User Author { get; set; }
public int LikesCount { get; set; }
public int DislikesCount { get; set; }
public int Time { get; set; }
public ICollection<Ingridient> Ingridients { get; set; }
public ICollection<RecipeStep> Steps { get; set; }
public Category Category { get; set; }
public int ServingsCount { get; set; }
public int Image { get; set; }
public decimal Price { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
public Recipe()
{
CreatedAt = DateTime.UtcNow;
UpdatedAt = DateTime.UtcNow;
}
}
所有型号都在这里。
运行dotnet ef migrations add InitialMigration
时,我只能迁移一个表Users
。这是生成的迁移的文本:
public partial class InitialMigration : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Users",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn),
Name = table.Column<string>(nullable: true),
CreatedAt = table.Column<DateTime>(nullable: false),
UpdatedAt = table.Column<DateTime>(nullable: false),
SocialId = table.Column<string>(nullable: true),
AccessToken = table.Column<string>(nullable: true),
RefreshToken = table.Column<string>(nullable: true),
ExpirationAccess = table.Column<DateTime>(nullable: false),
ExpirationRefresh = table.Column<DateTime>(nullable: false),
AuthType = table.Column<string>(nullable: true),
Money = table.Column<decimal>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Users", x => x.Id);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Users");
}
}
它看起来像是仅对Users
表的迁移。但是其余的呢?当我运行dotnet ef database update
时,我只有一个表Users
。
那怎么了?我是EF的新手,也许我不明白它的工作原理。您能描述一下如何从模型中生成所有表吗?
P.S。 该平台是.Net Core 2.2.300。项目类型是Web API。数据库是PostgreSQL。
我的数据库上下文是
public class ApplicationContext : DbContext
{
private readonly string _connectionString;
public ApplicationContext(IConfiguration configuration)
{
_connectionString = configuration.GetConnectionString("Recipes");
}
public DbSet<User> Users { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(_connectionString);
}
}
我已经通过标准DI系统注入了
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddEntityFrameworkNpgsql()
.AddDbContext<ApplicationContext>()
.BuildServiceProvider();
services.AddSingleton<IAuthService, AuthService>();
}
答案 0 :(得分:3)
您已经在DbContext中添加了一组User
。
public DbSet<User> Users { get; set; }
您尚未添加一组Recipe
。这就是为什么要迁移User
而不迁移Recipe
的原因。为配方添加一个,然后通过反转并重新创建来重新生成迁移,或者添加另一个以使更改复杂化。
public DbSet<Recipe> Recipes { get; set; }
注意:您可以 映射实体并进行迁移,而无需将DbSet<>
添加到DbContext
中,但这涉及在OnModelCreating
中设置实体。