Solved

Entity Framework using multiple contexts Edmx files?

Posted on 2014-12-18
7
475 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
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

829 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