问个ef core 投影问题

2021-01-26 12:19发布

public class Order {
public long Id { get; set; }
public string CustomerName { get; set; }
public string Address { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public bool Shipped { get; set; }

    public IEnumerable<OrderLine> Lines { get; set; } 
}

public class OrderLine
{
public long Id { get; set; }
public int ProductId { get; set; }
public Product Product { get; set; }
public int Quantity { get; set; }
public long OrderId { get; set; }
public Order Order { get; set; }
}

var products = this.productRepository.Products;
Order order = id == 0 ? new Order() : this.ordersRepository.GetOrder(id);

IDictionary<int, OrderLine> linesMap = order.Lines?.ToDictionary(l => l.ProductId) ?? new Dictionary<int, OrderLine>();

ViewBag.Lines = products.Select(p => linesMap.ContainsKey(p.Id)? linesMap[p.Id]: new OrderLine { Product = p, ProductId = p.Id, Quantity = 0 });

linesMap[p.Id] 也就是OrderLine,它的Product属性是如何被关联到数据库的内容的

标签: EF Core
1条回答
相关推荐>>
2楼-- · 2021-01-26 13:07

问题写的不清楚,建议把问题弄详细点,Order类也没有看到,

查看更多
登录 后发表回答