using EF Core IdentityContext and DbContext both for order management

这一生的挚爱 提交于 2020-01-14 19:17:28

问题


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.

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.

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

    }
}

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

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.

Please advice.


回答1:


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

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


来源:https://stackoverflow.com/questions/48623667/using-ef-core-identitycontext-and-dbcontext-both-for-order-management

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!