Mar*_*iel 6 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; }
}
Run Code Online (Sandbox Code Playgroud)
我还创建了继承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);
}
}
Run Code Online (Sandbox Code Playgroud)
我想迁移所有类并创建新数据库.我已经使用了 dotnet ef迁移添加了InitializeMigration来添加新的迁移,之后我想更新我的数据库,所以我使用了dotnet ef数据库更新,但Postgres抛出异常:
Npgsql.PostgresException: 42P01: relationship "ProjectState.IX_ProjectState_ProjectId" doesn't exist
Run Code Online (Sandbox Code Playgroud)
你能帮帮我吗?
就我而言,我只是用带有实际表名称的“Table”属性来装饰我的模型类(注意大小写敏感度),它解决了我的问题
[Table("city")]
public class CityModel
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2037 次 |
最近记录: |