简体   繁体   English

如何从多对多关系数据库中返回对象 - entityframework

[英]How to return objects from many to many relation Db - entityframework

Im trying to return a List of products of a specific user by user Id, but that seems to not work我试图按用户 ID 返回特定用户的产品列表,但这似乎不起作用

My Product class我的产品类别

public class Product
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
    public int Quantity { get; set; }
    
    public List<User>? Users { get; set; }
}

My user class我的用户类

public class User
{
    [Key]
    public Guid Id { get; set; }
    
    [Required] [MaxLength(15)]
    public string Username { get; set; }
    
    [Required]
    public string Name { get; set; }
    public string Surname { get; set; }
    public string PasswordHash { get; set; }
    public string Salt { get; set; }

    public List<Product>? Products { get; set; }
}

So im adding and Product to Db, this is working所以我将产品添加到 Db,这是有效的

And then im adding the product to Order by this method Guid id is a user's id然后我通过这种方法将产品添加到订单中 Guid id 是用户的 id

    public void AddProduct(Product product, Guid id)
    {
        var user = _context.Users.First(u => u.Id == id);

        var p = _context.Products.First(p => p.Id == product.Id);

        if (user.Products == null || p.Users == null)
        {
            user.Products = new List<Product>();
            p.Users = new List<User>();
        }
        
        user.Products.Add(p);
        p.Users.Add(user);
        
        _context.SaveChanges();
    }

And this also seems to work: image of ProductUser table from db这似乎也有效:来自 db 的 ProductUser 表的图像

So how can I return a List of Products which specific user have?那么如何返回特定用户拥有的产品列表呢?

I've tried this:我试过这个:

    private Order BuildOrder(Guid id)
    {
        var user = _context.Users.First(u => u.Id == id);

        /*if (user.Products is null)
        {
            user.Products = new List<Product>();
        }*/

        var x = _context.Products.Where(p => p.Id == 1);
        
        /*
        var products = user.Products.ToList();*/
        
        var order = new Order
        {
            Products = x.ToList()
        };

        return order;

But this is returning me null, like Adding Products is not working Result of this method但这使我返回 null,例如添加产品不起作用此方法的结果

Order class:订单类别:

public class Order
{
    public List<Product> Products { get; set; }
}

DbContext:数据库上下文:

using Application.Api.Models;
using Microsoft.EntityFrameworkCore;

namespace Application.Api.Data;

public class ApplicationContext : DbContext
{
    public ApplicationContext(DbContextOptions<ApplicationContext> options) 
        : base(options)
    {
        
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.UseSerialColumns();

        modelBuilder.Entity<User>(eb =>
        {
            eb.HasMany(u => u.Products).WithMany(p => p.Users);
        });

    }

    public DbSet<User> Users { get; set; }
    public DbSet<Product> Products { get; set; }
}

If that's not enough informations comment what I need to add如果这还不够信息,请评论我需要添加的内容

您需要为用户明确包含产品

var speceficUserWithProducts = context.Users.Include(u => u.Products).FirstOrDefault(u => u.Id == id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM