Entity Framework database partition - c#

I have extensive data and want to partition it into multiple databases based on date range.
How do I load the multiple DbContexts and combine them into one using Entity Framework? Is this possible?
TIA!

For example if u have three db's db1,2,3 have multiple connection strings and create a linked server of db2 and db3 to db1 so that db1 is your main db and create dbcontext for each db and make your db1 as your combined dbcontext of 2 and 3 .
Use repository pattern to inject these multiple databases.

Related

How to add new tables to existing database using migrations?

I use EF Core 5. I created a few entities in my code (like Products, Users etc.). I have an existing SQL database called DbClients. Using EFCodeFirst approach I would like to create and add tables (based on my entities) to my existing database DbClients. I see that in most cases you are able to create migrations and then create db using add-migration and update-database. The issue is that I don't want to cerate database. Just create a few tables to current db. What commend do I need to use to make it possible?

Use multiple huge databases in one Entity Framework Core query with SQL Server?

I want to query multiple tables from different databases in one single query with 1 or 2 dbcontext. I've been searching for some info, and it seems not easy to make it work perfectly.
I need to make several queries with 5 or 6 tables from 3 or 4 different databases at the same time.
I've tried using the implementation of a dbcontext into the another but I'm having a problem with the primary keys which I don't have if I only use one database.
I've tried 2 dbcontext separately, but I'm getting an error:
Cannot use multiple DbContext instances within a single query execution message.
I've read that I can relate the 2 databases, the thing is I cannot do that because of the structure of my enterprise, and it doesn't allow me to.
This is in Entity Framework Core 2.2 or 3, I already tried both. I have already the databases created, so I scaffolded them.
I have a single example, with only 2 databases:
var data =
from j in jsa.TbEmp
join i in info.TbcEmpGral on j.fiEmp equals i.fiEmp
where i.fiEmp == 361591
select i.FcName.ToString() + " - " + i.fiDate.ToString();
I want to query multiple tables from different databases [on one server] in one single query
The simplest way to do this is to create views or synonyms for all the tables in a single database. EF won't be able to scaffold the model automatically, but if you create the model by-hand EF will simply send SQL to that single database as if it contained all the tables, and SQL Server will redirect the queries to the appropriate objects through the synonyms or views.

Better way to work with multiple databases

So the problem is: I am working with a legacy project. Creating a web interface to databases. ASP.NET Core, CRUD, that usual stuff. BUT!
There are 3(three) databases. Let's say there are Companies and Employees tables in each one of them. And there are Companies in the first database which employees may be found in first, second and third databases.
Currently, I am writing custom queries to target multiple databases. Working directly with MySqlConnection classes and MySqlDataReader.
It gets the job done, but I am really missing Entity Framework's lazy loading
I know I can create multiple EF contexts, one for each database, but it's not gonna work because as I said - data may be spread out through all 3 DB.
Is there any better way to communicate with multiple databases?
You can create synonyms in one database for tables located in another database and try to use it in EF.

Entity Framework: how to use multple tables for the same entity?

My situation is as follows. I'm using entity framework (4.0) in which I have defined a relatively simple model like, let's say, two entities:
user
transaction
A user can have multiple transactions in this scenario. When generating the database this would result (obviously) in 2 database tables names 'user' and 'transaction'.
The problem is that I want to use the code as a webservice where different companies should have their own environment (read: own users and transactions). A simple solution would be to add a column to both tables like, lets say 'company_id' to identify one user/transactions for companya from the user/transaction from companyb.
What I would like to have is a duplication of the tables like so:
compa_user
compa_transaction
compb_user
compb_transaction
This way..all data would be nicely separated and if company a generates a lot of transactions, company b would not notice that the system is getting slow or whatsoever.
My question: is there a way to accomplish this based on entity framework. So, can I have multiple tables representing one entity in my model and switch from table to table depending on which company is connecting to the service.
Any help appreciated!
If you really want to keep the tables seperate, then a seperate database for each client would be the easiest - only need to change the connection string in EF.
The other benefit of this model (seperate databases) is that the database will scale quite easily as each database could theoretically be on a different database server should the DB ever become the bottleneck.

Associate tables from different databases - Entity Framework

I'd like to know how to (if possible) associate tables from different databases using entity framework. I have one edmx for my "xyz" database and another for my "abc" I need to associate them using EF. I know how to fix this using FKs, but that's not what I want, I'd like to be able to do that using the visual editor, not adding, by hand, a FK to my DB.
Here's a pratical example of what I'd like to accomplish:
Table User - database: abc
Table Message - database: xyz
I'd like to associate User to Message and vice-versa.
Could anyone help me?
Thanks!
You can query them together with Linq-to-Objects, but not with L2S or L2E.
A context is wrapped around 1 Db-connection, you cannot track/update entites from multiple db's.
A possible workaround is to 'link' to one of the Db's from the other. MS-SQL can do this. To the EF it would appear 1 database.
There is no way to achieve this using the edmx design surface in EF.
You could encapsulate this association within your domain model and provide a distributed transaction across the two EF Contexts. We've implemented the latter with an EF Context "container" class that given an internal collection of contexts uses a TransactionScope when it contains more than one context (this relies on the IUnitOfWork abstraction to work effectively).

Categories

Resources