Solved

Entity Framework using multiple contexts Edmx files?

Posted on 2014-12-18
7
501 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 40509829
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
ID: 40513013
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
ID: 40513158
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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

Author Comment

by:sbornstein2
ID: 40513193
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
ID: 40513200
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
ID: 40513208
okay tx
0
 

Author Closing Comment

by:sbornstein2
ID: 40533456
thanks for your time
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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