?
Solved

Entity Framework using multiple contexts Edmx files?

Posted on 2014-12-18
7
Medium Priority
?
573 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

Question has a verified solution.

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

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

800 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