Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core Relation doesn't exist

I want to create a database in PostgreSQL and API for my app. I've created Model and API in EF Core 1.0.0. Model exists in 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 have created also DemoContext that inherits DbContext. I defined relashionships in OnModelCreating(ModelBuilder modelBuilder) method.

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 migrate all classes and create new database. I've used dotnet ef migrations add InitializeMigration to add new migration and after it I wanted to update my database, so I've used dotnet ef database update but Postgres throw an Exception:

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

Can you help me with it?

like image 515
Marcin Gurbiel Avatar asked Aug 12 '16 11:08

Marcin Gurbiel


People also ask

How do I add a foreign key to EF core?

The [ForeignKey(name)] attribute can be applied in three ways: [ForeignKey(NavigationPropertyName)] on the foreign key scalar property in the dependent entity. [ForeignKey(ForeignKeyPropertyName)] on the related reference navigation property in the dependent entity.

Can I use EF in .NET core?

There's currently no support for using the EF designer directly on . NET Core or . NET Standard projects.

What is lazy loading EF core?

Lazy loading means that the related data is transparently loaded from the database when the navigation property is accessed.


1 Answers

In my case i just decorate my model class with "Table" attribute with actual table name (mind case sensativity) and it resolved my problem

[Table("city")]
public class CityModel
like image 173
Rajesh Kumar Bhawsar Avatar answered Oct 19 '22 10:10

Rajesh Kumar Bhawsar