我有以下一组模型:
用户:
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的新手,也许我不明白它的工作原理。您能描述一下如何从模型中生成所有表吗?
PS该平台是.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>();
}
您已将一组添加User
到您的DbContext中。
public DbSet<User> Users { get; set; }
您尚未添加一组Recipe
。这就是为什么要进行迁移User
而没有的原因Recipe
。为配方添加一个,然后通过反转并重新创建来重新生成迁移,或者添加另一个以使更改复杂化。
public DbSet<Recipe> Recipes { get; set; }
注意:您可以在不添加DbSet<>
的情况下映射和迁移实体,DbContext
但这需要在中设置实体OnModelCreating
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句