EF Core Ratio Does Not Exist

I want to create a database in PostgreSQL and an API for my application. I created a model and API in EF Core 1.0.0. The model exists in a separate library.

public class Architect : IEntity
{
    public Guid Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string PhoneNumber { get; set; }
    public string Email { get; set; }
    public ICollection<Project> Projects { get; set; }
}

public class Project : IEntity
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    [JsonConverter(typeof(IsoDateTimeConverter))]
    public DateTimeOffset CreatedAt { get; set; }
    public ICollection<Company> Companies { get; set; }
    public string City { get; set; }
    public ICollection<ProjectState> ProjectStates { get; set; }
    public Guid ArchitectId { get; set; }
    public Architect Architect { get; set; }
}

public class Company : IEntity
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public Guid ProjectId { get; set; }
    public Project Project { get; set; }
}

 public class ProjectState : IEntity
{
    public Guid Id { get; set; }
    public ProjectPhase phase { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public Guid ProjectId { get; set; }
    public Project Project { get; set; }
}

I also created a DemoContext that inherits DbContext. I defined relashionships in the OnModelCreating method (ModelBuilder modelBuilder).

public class DemoContext : DbContext
{
    public DbSet<Architect> Architects { get; set; }
    public DbSet<Project> Projects { get; set; }
    public DbSet<Company> Companies { get; set; }
    public DbSet<ProjectState> ProjectStates { get; set; }

    public DemoContext(DbContextOptions<DemoContext> options) : base(options) { }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseNpgsql("PORT = 5432; HOST = localhost; TIMEOUT = 15; POOLING = True; MINPOOLSIZE = 1; MAXPOOLSIZE = 20; COMMANDTIMEOUT = 20; DATABASE = ProgressTest; PASSWORD = 'postgres'; USER ID = postgres", b => b.MigrationsAssembly("WebAPI.API"));
    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.HasPostgresExtension("uuid-ossp");

        modelBuilder.Entity<Project>()
            .HasOne(p => p.Architect)
            .WithMany(a => a.Projects)
            .HasForeignKey(p=>p.ArchitectId);

        modelBuilder.Entity<ProjectState>()
            .HasOne(p => p.Project)
            .WithMany(p => p.ProjectStates)
            .HasForeignKey(p => p.ProjectId);

        modelBuilder.Entity<Company>()
            .HasOne(c => c.Project)
            .WithMany(p => p.Companies)
            .HasForeignKey(c => c.ProjectId);
    }
}

I want to transfer all classes and create a new database. I used dotnet ef migrations adds InitializeMigration to add a new migration, and after that I wanted to update my database, so I used the dotnet ef database update, but Postgres threw an exception:

Npgsql.PostgresException: 42P01: relationship "ProjectState.IX_ProjectState_ProjectId" doesn't exist

Can you help me?

+4
source share

Source: https://habr.com/ru/post/1651056/


All Articles