首页 > 解决方案 > 无法在 postgresql 中创建表

问题描述

我在 pgAdmin 中手动删除了两个表,现在我想再次创建它。我做了一些更改,应用了迁移,但未创建此表。无法理解,为什么?

当我发送将项目添加到此表的请求时,我收到错误:关系“ProductsComparsion”不存在

在此处输入图像描述

public class Product
{
    public int Id { get; set; }
    public ICollection<ProductToCompare> ProductsToCompare { get; set; } //! Added
}
// I want to create tables for two models below
public class ProductComparsion
{
    public int Id { get; set; }
    public int? UserId { get; set; }
    public Guid SessionId { get; set; }
    public int CategoryId { get; set; }
    public DateTime Created { get; set; }
    public ICollection<ProductToCompare> ProductsToCompare { get; set; }
}

public class ProductToCompare
{
    public int ProductComparsionId { get; set; }
    public ProductComparsion ProductComparsion { get; set; }
    public int ProductId { get; set; }
    public Product Product { get; set; }        
}

AppDbContextModelProvider

modelBuilder.Entity<ProductComparsion>(typeBuiler =>
{
    typeBuiler.ToTable(nameof(AppDbContext.ProductsComparsion));
    typeBuiler.HasKey(z => z.Id);
});

modelBuilder.Entity<ProductToCompare>(typeBuilder =>
{
    typeBuilder.HasKey(z => new { z.ProductId, z.ProductComparsionId });
    typeBuilder.ToTable(nameof(AppDbContext.ProductsToCompare));                
    typeBuilder.HasOne(z => z.ProductComparsion).WithMany(z => z.ProductsToCompare).HasForeignKey(z => z.ProductComparsionId);  
    typeBuilder.HasOne(z => z.Product).WithMany(z => z.ProductsToCompare).HasForeignKey(z => z.ProductId);
});

在此处输入图像描述

标签: c#asp.netpostgresqlentity-frameworkpgadmin

解决方案


推荐阅读