Solved

Entity Framework using multiple contexts Edmx files?

Posted on 2014-12-18
7
533 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
Technology Partners: 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 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

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!

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

696 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