Print Page | Close Window

Two Entities and Two DataBase problem

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2482
Printed Date: 04-May-2025 at 3:25am


Topic: Two Entities and Two DataBase problem
Posted By: Molinari
Subject: Two Entities and Two DataBase problem
Date Posted: 02-Feb-2011 at 7:38am
I have two entities and two database... Consequently.. two EM..

EntitiesFirst _em = new EntitiesFirst();
_em.DefaultQueryStrategy = QueryStrategy.DataSourceOnly;

EntitiesSecond _em2 = new EntitiesSecond();
_em2.DefaultQueryStrategy = QueryStrategy.Normal;



When I use EntitiesFirst.. sucess but when i use EntitiesSecond..

public static void Test()
{
   EntitiesSecond _em2 = EntitiesSecond.DefaultManager;

_em2.Employer.Where(c=>c.id == 1)....
}

Show Error...
Type conflict: the DefaultManager is currently of type EntitiesFirst

how to use it properly.

Thanks





Replies:
Posted By: DenisK
Date Posted: 02-Feb-2011 at 3:31pm
Hi Mollinary;

The DefaultManager is a static shared property across all EntityManager(s). This means that the following are the same instance.

EntityManager.DefaultManager
EntitiesFirst.DefaultManager
EntitiesSecond.DefaultManager

Which means that before you get the DefaultManager and assign it to a different type, you need to set it to the proper type first as follows:

EntityManager.DefaultManager = new EntitiesSecond();
EntitiesSecond _em2 = EntitiesSecond.DefaultManager;

Hope this helps.


Posted By: Molinari
Date Posted: 03-Feb-2011 at 4:28am
Hi Denisk,

Then mean that I can never use the cache on EntitiesSecond. because I need set   NEW EntityManager.DefaultManager = new EntitiesSecond();
every time before executing a query ? Or has some way to use this cache?

thanks..


Posted By: DenisK
Date Posted: 03-Feb-2011 at 11:11am
Hi Molinari;

No, you don't need to set it to new EntitiesSecond() every time. You can set it to any existing EntityManager of type EntitiesSecond and use its cache.


Posted By: Molinari
Date Posted: 03-Feb-2011 at 11:40am
Hi Denisk,

Ok.. but I need using two database..and project has only one EntityManager so when i need to run the query EntitiesFirst() I set in EntityManager after I need execute the query EntitiesSecond() then set new again...

I create one Test .... every time change EntityManader.DefaultManager.... cache is Empty...

if you can post the code...for example..I thank


Posted By: DenisK
Date Posted: 03-Feb-2011 at 11:52am
Hi Molinari;

Please see code example below.

[TestMethod]
    public void UsingDefaultManager() {      
      var mgr1 = new NorthwindIBEntityManager();
      var mgr2 = new FantasyFootballEntityManager();

      var a = mgr1.Employees.FirstOrNullEntity();
      var b = mgr2.Clubs.Take(9).ToList();

      var default1 = NorthwindIBEntityManager.DefaultManager;
      EntityManager.DefaultManager = mgr2;
      var default2 = FantasyFootballEntityManager.DefaultManager;
      var clubInCache = default2.FindEntities<Club>(EntityState.AllButDetached).ToList();
    }

The last query finds all entities of type Club in cache. The results are 9 clubs which is correct because the previous query

mgr2.Clubs.Take(9).ToList();

takes 9 Club entities and put them in cache.



Print Page | Close Window