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?
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.
There's currently no support for using the EF designer directly on . NET Core or . NET Standard projects.
Lazy loading means that the related data is transparently loaded from the database when the navigation property is accessed.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With