0

I have a model that I want to load one of its columns from a source other than the database behind EF Core for example MongoDb.

The problem is that I can't figure how to override query execution in EF Core. For Saves it's OK because EF Core lets us to override SaveChanges and do our own logic.

For the read part, I decided to load the column lazily through its getter accessor. The goal is to have as little as possible change in our code (We have lots of projects that uses EF Core to load the object and I don't want to change code in a way that breaks all queries of that specific object).

But how to inject my MongoDb service to my model? For ASP.Net Core, we can create a custom model binder but for EF Core how to have a custom model binder or model creator to inject services to model? Can I use dependency injectors like Ninject or AutoFac (I just heard about them and don't have any experience with them)? Do they work with EF Core?

SSgumS
  • 76
  • 7

2 Answers2

0

Your design is fundamentally broken. EF Core is an ORM for relational databases, and specifically can only natively work with one database per context. Composing from multiple data sources is not something you should be doing through EF Core code, but rather through some sort of service class that internally utilizes EF Core and the MongoDB driver. Your app code should, then utilize this service, and not EF Core directly. It is 100% the wrong approach to trying to hack this into your context code.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • I know that it's not supported in ef core and we should have repository or any other things to wrap mongodb and ef core context and use it as service. but It will have a lots of work and probably lots of breaking changes. I tried to modify ef core because I didn't want to change whole structure of my app. I have 20 projects based on this project that I am editing. If I make such a change, It will break all of them. – SSgumS Apr 13 '20 at 16:09
  • *Then break them.* Seriously. Your design is fundamentally flawed, so you're now building on a house of cards, accumulating more and more technical debt the further you go down the rabbit hole. The time to do it right is *now*. You're a developer. This is your job. – Chris Pratt Apr 13 '20 at 16:22
0

You probably have to do the mapping in memory. I don't know if that is viable in your specific scenario. That means you would have to eagerload and then you could do mapping with auto mapper or another mapper. You would need an abstraction on top of ef where you could inject the mapping mechanism. The Ef core link provider would not be able to translate what you need to a SQL query.