一对一关系不承载两个类

我正在使用实体框架来保存和检索一些信息,我有一个一对一的关系。产品和类别,其中一个产品有一个类别,一个类别可能有多个产品。


我有以下结构


我创建了这两个实体并建立了关系,但是当我检索此信息时,它给我带来了产品的信息,但是类别为空


产品.cs


public class Produto 

{

    [Key]

    public int id { get; set; }

    public string descricao { get; set; }

    public string observacao { get; set; }

    public int status { get; set; }

    public decimal valorVenda { get; set; }

    public virtual Categoria categoria { get; set; }

}

类别.cs


public class Categoria 

{

    [Key]

    [ForeignKey("categoriaid")]

    public int id { get; set; }

    public string descricao { get; set; }

    public string observacao { get; set; }

    public int status { get; set; }

    public virtual Produto produto { get; set; }

}

ProductContext.cs


public class ProdutoContexto : DbContext 

{

    public ProdutoContexto(DbContextOptions<ProdutoContexto> options) : base(options)

    {

    }


    protected override void OnModelCreating(ModelBuilder modelBuilder)

    {

        modelBuilder.Entity<Produto>()

            .HasOne(a => a.categoria)

            .WithOne(b => b.produto)

            .HasForeignKey<Categoria>(b => b.id);

    }


    public DbSet<Produto> Produtos { get; set; }

}

分类上下文.cs


public class CategoriaContexto : DbContext 

{

    public CategoriaContexto(DbContextOptions<CategoriaContexto> options) : base(options)

    {

    }

    public DbSet<Categoria> Categorias { get; set; }

}

当我运行函数来检索信息时,返回以下 json


[{"id":1,"descricao":"Coca-Cola","observacao":"Coca-Cola Gelada","status":1,"valorVenda":5.50,"categoria":null}]

我的查询是:


[HttpGet] 

public async Task<ActionResult<IEnumerable<Produto>>> GetProdutos() 

   return await _context.Produtos.ToListAsync(); 

}

注意category是null,怎么能做到已经加载了category呢?


萧十郎
浏览 84回答 1
1回答

撒科打诨

类别可能有多个产品。那么它不是one-to-one,而是它one-to-many和你的模型类应该如下所示:public class Categoria&nbsp;{&nbsp; &nbsp; [Key]&nbsp; &nbsp; public int id { get; set; }&nbsp; &nbsp; public string descricao { get; set; }&nbsp; &nbsp; public string observacao { get; set; }&nbsp; &nbsp; public int status { get; set; }&nbsp; &nbsp; public virtual ICollection<Produto> produtos { get; set; }}public class Produto&nbsp;{&nbsp; &nbsp; [Key]&nbsp; &nbsp; public int id { get; set; }&nbsp; &nbsp; [ForeignKey("categoria")]&nbsp; &nbsp; public int categoriaId {get; set;}&nbsp; &nbsp; public string descricao { get; set; }&nbsp; &nbsp; public string observacao { get; set; }&nbsp; &nbsp; public int status { get; set; }&nbsp; &nbsp; public decimal valorVenda { get; set; }&nbsp; &nbsp; public virtual Categoria categoria { get; set; }}而且您不需要任何FluentAPI配置。所以删除modelBuilder.Entity<Produto>()配置。而且您也不需要分别为Produto和两个不同的 DbContext Categoria。而是让你DbContext的如下:public class ApplicationDbContexto : DbContext&nbsp;{&nbsp; &nbsp; public ApplicationDbContexto(DbContextOptions<ApplicationDbContexto> options) : base(options)&nbsp; &nbsp; {&nbsp; &nbsp; }&nbsp; &nbsp; public DbSet<Categoria> Categorias { get; set; }&nbsp; &nbsp; public DbSet<Produto> Produtos { get; set; }}您的查询应如下所示:[HttpGet]&nbsp;public async Task<ActionResult<IEnumerable<Produto>>> GetProdutos()&nbsp;{&nbsp;&nbsp; &nbsp;return await _context.Produtos.Include(p => p.categoria).ToListAsync();&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP