Select with two context in entityframework

Asked

Viewed 86 times

0

I need to make a comparison, from two databases, I need to bring the emails that do not exist in the other bank. I tried to do it this way:

var email = _contextVO.Usuarios.Where(x => x.Ativo == true && !_userManager.Users.Where(a => a.Email == x.Email).Any()).ToListAsync();

But he returns this error to me:

Cannot use Multiple Dbcontext instances Within a single query Execution. Ensure the query uses a single context instance

I thought of separating, but I couldn’t compare! I can’t work this way with two context ? How can I get around this problem. 'Cause I’m gonna need to enter the Banco1 data for Banco2.

1 answer

1


Yes, for each context you need to bring a list, and only then do the comparisons;

If I had all the attributes of these classes, I could do a better query, with what was given, I would look like this:

//buscando os dados em _contextVO
var Lista_Usuarios = (from u in _contextVO.Usuarios
            Where(u.Ativo)
            select u).toList();

//buscando os dados em _userManager          
var Lista_2 = (from u in _userManager.Users).toList();
//comparando
var Lista_Filtrada  = Lista_Usuarios.Where(w=>!Lista_2.contains(w.email));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.