为什么在运行实体框架迁移后我只有一个表?

Aleksej_Shherbak

我有以下一组模型:

用户:

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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么只有我的第一个HTTP请求运行?

来自分类Dev

当我尝试关联现有实体时,为什么实体框架会创建一个新实体?

来自分类Dev

如何在实体框架中拥有一个只有外键的表

来自分类Dev

为什么我的电子表格在运行我的脚本后显示来自另一个工作表的幽灵图像

来自分类Dev

为什么我有一个 elasticsearch modulenotfounderror 即使它正在运行?

来自分类Dev

为什么在运行一个函数后就停止了,为什么这个变量没有上升?

来自分类Linux

确保Bash脚本只有一个实例正在运行的最佳方法是什么?

来自分类Dev

为什么我的5个测试中只有2个在我的Google C ++测试框架上运行

来自分类Dev

为什么我的代码在运行时给我一个stackdump错误?

来自分类Dev

为什么实体框架为我的视图模型创建一个区分符列?

来自分类Dev

为什么valgrind说如果我使用2个整数,则只有一个alloc?

来自分类Java

货币转换器 - 为什么我在交换机不会工作循环?当我运行我的代码只有第一个for循环工程

来自分类Dev

克隆后,我只有一个分支吗?

来自分类Java

为什么我只有一个Calendar对象实例

来自分类Dev

为什么只有我在 onClick 中的一个函数执行?

来自分类Dev

为什么即使我使用.each也只有一个元素触发动作?

来自分类Dev

为什么我要创建一个只有`PhantomData <()>成员的结构?

来自分类Dev

为什么只有我的最后一个本地通知函数被调用?

来自分类Dev

为什么我在用 sklearn 进行线性回归时只有一个 coef_?

来自分类Dev

为什么我的for循环在python列表迭代器之后只有一个结果?

来自分类Dev

为什么只有一个协程在我的代码中起作用?

来自分类Dev

为什么我的initrd只有一个目录,即“内核”?

来自分类Dev

为什么我的任务管理器性能图中只有一个核心?

来自分类Dev

为什么我的元组列表只有一个条目?

来自分类Dev

为什么只有一个按钮?

来自分类Python

为什么只有一个警告循环?

来自分类Java

为什么每个文件只有一个类

来自分类Dev

为什么此查询只有一个结果?

来自分类Dev

为什么只有最后一个输入是存储?

Related 相关文章

  1. 1

    为什么只有我的第一个HTTP请求运行?

  2. 2

    当我尝试关联现有实体时,为什么实体框架会创建一个新实体?

  3. 3

    如何在实体框架中拥有一个只有外键的表

  4. 4

    为什么我的电子表格在运行我的脚本后显示来自另一个工作表的幽灵图像

  5. 5

    为什么我有一个 elasticsearch modulenotfounderror 即使它正在运行?

  6. 6

    为什么在运行一个函数后就停止了,为什么这个变量没有上升?

  7. 7

    确保Bash脚本只有一个实例正在运行的最佳方法是什么?

  8. 8

    为什么我的5个测试中只有2个在我的Google C ++测试框架上运行

  9. 9

    为什么我的代码在运行时给我一个stackdump错误?

  10. 10

    为什么实体框架为我的视图模型创建一个区分符列?

  11. 11

    为什么valgrind说如果我使用2个整数,则只有一个alloc?

  12. 12

    货币转换器 - 为什么我在交换机不会工作循环?当我运行我的代码只有第一个for循环工程

  13. 13

    克隆后,我只有一个分支吗?

  14. 14

    为什么我只有一个Calendar对象实例

  15. 15

    为什么只有我在 onClick 中的一个函数执行?

  16. 16

    为什么即使我使用.each也只有一个元素触发动作?

  17. 17

    为什么我要创建一个只有`PhantomData <()>成员的结构?

  18. 18

    为什么只有我的最后一个本地通知函数被调用?

  19. 19

    为什么我在用 sklearn 进行线性回归时只有一个 coef_?

  20. 20

    为什么我的for循环在python列表迭代器之后只有一个结果?

  21. 21

    为什么只有一个协程在我的代码中起作用?

  22. 22

    为什么我的initrd只有一个目录,即“内核”?

  23. 23

    为什么我的任务管理器性能图中只有一个核心?

  24. 24

    为什么我的元组列表只有一个条目?

  25. 25

    为什么只有一个按钮?

  26. 26

    为什么只有一个警告循环?

  27. 27

    为什么每个文件只有一个类

  28. 28

    为什么此查询只有一个结果?

  29. 29

    为什么只有最后一个输入是存储?

热门标签

归档