使用 EF Core IdentityContext 和 DbContext 进行订单管理

using EF Core IdentityContext and DbContext both for order management

我正致力于在 ASP MVC Core 2 上创建一个电子商务网站。我从 IdentityUser 继承了我的用户并从 IdentityDbContext 继承了用于处理用户数据的上下文,并继承了一个不同于 DbContext 用于处理产品和订单等的上下文

现在,我想 link 向特定用户发送订单或购物车,我无法思考如何按订单 table 引用用户,因为他们处于不同的上下文中.我还在 tables.

中使用 EF 创建的默认 guid 作为主键

我应该放弃 DbContext 并只使用 IdentityDbContext 吗?这样做是否会导致身份中的异步方法和其他常用的非异步方法出现问题。

这是我的 classes

中的一些代码片段
using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace shophe1.Models
{
    public enum Gender { Male, Female, Other}
    public class User : IdentityUser
    {
        public string FullName { get; set; }
        public Gender Gender { get; set; }
        public string ReferralID { get; set; }
        public DateTime RegistrationDateTime { get; set; }

        public string ActivationDateTime { get; set; }

        public string UserType { get; set; }
        public Wallet Wallet {get;set;}


        public virtual ICollection<UserBankDetail> BankDetails { get; set; }
        public virtual ICollection<UserAddress> AddressDetails { get; set; }
       //public ShoppingCart Cart { get; set; }
        //public ICollection<Order> Orders { get; set; }

    }
}

订单class

using System.Collections.Generic;

namespace shophe1.Models
{
    public class Order
    {
        public string OrderId { get; set; }
        public ICollection<product> CartProducts { get; set; }
        //public User User { get; set; }
        public decimal OrderTotal { get; set; }
        public decimal ShippingCharges { get; set; }
    }
}

问题是,如果我在订单模型中添加用户并在用户 class 中添加订单集合,两种上下文都会混淆,并且在迁移时,所有与用户和产品相关的模型都会在同一迁移中出现,无论我是否在迁移中使用 DbContext 或 IdentityContext --context 选项。这是因为用户和订单现在是相互关联的。

请指教

IdentityDbContext 继承您的上下文。理想情况下,每个数据库都应该有一个上下文 - 特别是如果您想将实体彼此关联起来。

如果您真的想要单独的数据库,例如身份表驻留在一个数据库中,而您的应用程序表驻留在另一个数据库中,那么您将无法直接将实体彼此关联起来。但是,您仍然可以创建一个伪外键,您只需将特定用户的 ID 存储在您的一个实体的列中。然后,您只需要使用此 ID 在另一个上下文中发出单独的查询以手动获取用户。例如:

var order = await appContext.Orders.FindAsync(orderId);
var user = await identityContext.Users.FindAsync(order.UserId);