none
Multiple DB Context and join across context

    Question

  • I am new to EF Core and we are trying to figure out the best way to organize our entities and DBContext across different applications.  An example would be if we were a car dealership, we would have a "sales" account DBContext and a "repair" DBContext.  We have an app for "sales" and an app for "repair".  However, there is one need where the "sales" guy needs to look at at a car that was traded in to see what repairs were done here.  In that case he needs to join sales with repairs.

    My question is how can you join entities across DBContext?  If this is not possible, then what is the best way of managing scenarios like this so that you don't put all your entities in the same DBContext for all your apps?

    Tuesday, April 2, 2019 6:50 PM

All replies

  • Hi,

    There is no need to create a DBContext for each table. There can be multiple different tables in a DBContext.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 3, 2019 8:42 AM
  • Hi Ohoover,

    I think what you need here is bounded context, here is a good article about the topic Data Points - Shrink EF Models with DDD Bounded Contexts

    for example, you can use a constructor in your DbContext to load specific types and thus a specific model for your use case: 

    public class AppDataContext: DbContext
    {
        private Type[] _types;
        public AppDataContext(string connectionString = "", params Type[] types) : base(connectionString)
        {
            if(types != null)
            {
                _types = types;
            }
        }
    
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            if(_types != null)
            {
                _types
                    .ToList()
                    .ForEach(t => {
                        modelBuilder.RegisterEntityType(t);
                    });                
            }
    
            base.OnModelCreating(modelBuilder);
        }
    }


    and then on the model for your specific use case you have to mark types to exclude using the NotMappedAttribute attribute:

    public class UserSpecificModel
    {
        public int Id { get; set; }
        public string username { get; set; }
        public string email { get; set; }
        [NotMapped]
        public ICollection<Order> Orders{ get; set; }
    }

    and then use the context this way:

    using (var context = new AppDataContext(connectionString), typeof(UserSpecificModel)) {
        context.Set<UserSpecificModel>().ToList() 
        // specific operations for your use case
    }

    Hope it helps,

    Good Coding;



    Wednesday, April 3, 2019 8:49 AM
  • My question is how can you join entities across DBContext? 

    I don't think EF Core is any different than the previous versions of EF where you cannot join across context.

    If this is not possible, then what is the best way of managing scenarios like this so that you don't put all your entities in the same DBContext for all your apps?

    You can use the non generic repository pattern, becuase a repository can communicate with another repository.

    https://martinfowler.com/eaaCatalog/repository.html

    https://programmingwithmosh.com/net/common-mistakes-with-the-repository-pattern/

    https://blog.sapiensworks.com/post/2012/03/05/The-Generic-Repository-Is-An-Anti-Pattern.aspx

    You can use the DAO pattern, becuase DAO(s) can communicate with each other.

    https://blog.sapiensworks.com/post/2012/11/01/Repository-vs-DAO.aspx

    https://en.wikipedia.org/wiki/Data_access_object

    https://www.tutorialspoint.com/design_pattern/data_access_object_pattern.htm

    You can use the DTO pattern with repository and DAO patterns.

    https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp

    By doing one  the above,  the EF context is segregated into a context per repository or a context per DAO.

    Wednesday, April 3, 2019 11:45 AM