Solved

Entity Framework using multiple contexts Edmx files?

Posted on 2014-12-18
7
396 Views
Last Modified: 2015-01-06
Hey all,

So I have a issue I am trying to resolve where we have a Core database which I have an edmx a DBContext etc.   Problem I have is we are storing keys from another database into our database to do Lookups to the second database.   The good thing is they exist on the same server just two databases.   I have been doing some reading this morning on setting up synonyms etc. but I am trying to see is there a simpler way where I can have two and just use LINQ to join the entities.  I am using repository pattern and unit of work as well.   When I create my repositories it seems to recognize the entities across the Edmx files which I found strange even though I have one context right now.  But the issue is when in the service I do the join I get the error:

The entity type Country is not part of the model for the current context.

I have a composits folder in my app that has a Context.cs file defined that is like this for the Billing Database but then I have a second Edmx that has a Customer Database connection string:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClientBilling.DataAccess.Contracts
{
    public partial class ClientBillingEntities : DbContext
    {

        public ClientBillingEntities(bool traceWinUser)
        {
            string ma = System.Environment.MachineName;

            string userName = System.Web.HttpContext.Current == null? String.Empty : string.Format("{0}", System.Web.HttpContext.Current.User.Identity.Name);

            string tempStr = Database.Connection.ConnectionString;
            Database.Connection.ConnectionString = tempStr.Replace("EntityFramework", string.Format("BillingDatabase.[{0}]", userName)); ;
        }
        

        public override int SaveChanges()
        {
            return base.SaveChanges();
        }

        [Initialize]
        public void RandomMethod()
        {
        }

        [AttributeUsage(AttributeTargets.Method)]
        public class InitializeAttribute : Attribute
        {

        }
    }
}
namespace System.Data.Common
{
    public partial class DbConnection
    {
        string str;
        public string ConnectionString 
        {
            get
            {
                return str;
            }
            set
            {
                str = value;
            }
        }
    }
}

Open in new window

Is this a bad approach and is there a way to have two contexts that use the two separate connection strings?
0
Comment
Question by:sbornstein2
  • 4
  • 3
7 Comments
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
I am confused...

Do you have 2 separate databases, with 2 separate connection strings?  

Does each DataContext connect to a different database?
0
 

Author Comment

by:sbornstein2
Comment Utility
Sorry Bob I answered the wrong question here.   Yes it is 2 separate databases so 2 separate connection strings.   Yes each context needs to connect to a different database then I will do Linq joins.   I am trying to avoid using views.
0
 
LVL 96

Expert Comment

by:Bob Learned
Comment Utility
1) With EF, I use the EntityConnectionStringBuilder:

How to: Build an EntityConnection Connection String
http://msdn.microsoft.com/en-us/library/vstudio/bb738533(v=vs.100).aspx

2) The DbContext has a constructor that takes an EntityConnection.

3) Creating an EntityConnection from the connection string in step #1.

using (EntityConnection conn = new EntityConnection(entityBuilder.ToString()))
{
    conn.Open();

...

Open in new window

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:sbornstein2
Comment Utility
So would I need two DBContext then in this case where each would have a different connection string?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
Comment Utility
I would create a different DB context for each database requirement, and match the connection string to the context.  That way, you partition the data requirements, so you know where entities are defined.
0
 

Author Comment

by:sbornstein2
Comment Utility
okay tx
0
 

Author Closing Comment

by:sbornstein2
Comment Utility
thanks for your time
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now