Reputation: 3058
I have the following set of models:
User:
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);
}
}
Recipes:
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;
}
}
All models is here.
When I running dotnet ef migrations add InitialMigration
I have migration for creating only one table Users
. Here is the text of the migration that was generated:
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");
}
}
It looks like as migrations for only Users
table. But where is the rest? And when I'm running dotnet ef database update
I have only one table Users
.
So what is wrong? I'm newbie in EF, and maybe I don't understand how it works. Can you describe me how to generate all tables from my models?
P.S. The platform is .Net Core 2.2.300. Project type is Web API. Database is PostgreSQL.
My db context is
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);
}
}
I've injected it via standard DI system
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddEntityFrameworkNpgsql()
.AddDbContext<ApplicationContext>()
.BuildServiceProvider();
services.AddSingleton<IAuthService, AuthService>();
}
Upvotes: 1
Views: 266
Reputation: 13972
You've added a set of User
to your DbContext.
public DbSet<User> Users { get; set; }
You've not added a set of Recipe
. That's why you have a migration for User
but not Recipe
. Add one for recipe and then either regenerate the migration by reversing it and recreating it, or add another to compound the changes.
public DbSet<Recipe> Recipes { get; set; }
Note: You can get entities mapped and migrating without adding DbSet<>
to the DbContext
but it involves setting up the entity in OnModelCreating
.
Upvotes: 3