繁体   English   中英

EF 核心关系不存在

[英]EF Core Relation doesn't exist

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

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

我想迁移所有类并创建新数据库。 我使用了 do.net ef migrations add InitializeMigration来添加新的迁移,之后我想更新我的数据库,所以我使用了 do.net ef 数据库更新但是 Postgres 抛出异常:

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

你能帮我吗?

在我的例子中,我只是用实际表名(注意区分大小写)的“表”属性装饰我的 model class,它解决了我的问题

[Table("city")]
public class CityModel

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM