同时使用EF Core IdentityContext和DbContext进行订单管理 [英] using EF Core IdentityContext and DbContext both for order management

查看:409
本文介绍了同时使用EF Core IdentityContext和DbContext进行订单管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on creating an ecommerce website on ASP MVC Core 2. I inherited my user from IdentityUser and inherited context from IdentityDbContext for working with user data and inherited a different context from DbContext for working with products and orders etc.

现在,我想将订单或购物车链接到特定用户,并且无法绕过如何在订单表中引用该用户的想法,因为他们处于不同的上下文中.我还在两个表中都使用了EF创建的默认guid作为主键.

Now, I want to link an order or shopping cart to a particular user and can not wrap my head around how to refer to the user in order table as they are in different contexts. I am also using the default guid created by EF as primary key in both the tables.

我应该抛弃DbContext并仅使用IdentityDbContext吗?这样做是否会导致标识中的异步方法和其他常见的非异步方法出现问题.

Should I ditch DbContext and use IdentityDbContext only? Does doing this causes problems with async methods in identity and other usual non async methods.

这是我班上的一些代码段

Here are some code snippets from my 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; }

    }
}

订单类别

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; }
    }
}

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

The issue is if I add user in order model and a collection of orders in user class, both contexts get mixed up and when migrating, all user and product related models get clubbed in same migration whether I use DbContext or IdentityContext in migrations --context option. This is because both user and orders are now interrelated.

请咨询.

推荐答案

IdentityDbContext继承上下文.理想情况下,每个数据库应该有一个上下文-尤其是当您想要将实体彼此关联时.

Inherit your context from IdentityDbContext. You should have one context per database, ideally - especially if you want to relate the entities to each other.

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

If you actually want separate databases, such that the Identity tables reside in one, while your application tables reside in another, then you won't be able to directly relate the entities with each other. However, you can still create a pseudo-foreign key, where you simply store the id of a particular user in a column on one of your entities. You'd then merely need to issue a separate query on the other context with this id to fetch the user manually. For example:

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

这篇关于同时使用EF Core IdentityContext和DbContext进行订单管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆