EF核心关系不存在

时间:2016-08-12 11:10:50

标签: c# postgresql entity-framework-core

我想在我的应用程序中为PostgreSQL和API创建一个数据库。我在EF Core 1.0.0中创建了Model和API。模型存在于单独的库中。

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; }
}

我还创建了继承DbContext的DemoContext。我在OnModelCreating(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);
    }
}

我想迁移所有类并创建新数据库。我已经习惯了 dotnet ef迁移添加了InitializeMigration 以添加新迁移,之后我想更新我的数据库,因此我使用了 dotnet ef数据库更新但Postgres抛出异常:

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

你能帮帮我吗?

0 个答案:

没有答案