How to write cross database query in entity framework [duplicate] - c#

This question already has answers here:
Can code-first Entity Framework do cross database queries with SQL Server DBs on the same box?
(4 answers)
Closed 8 years ago.
I am using entity framework 6.1 and I have two databases.
CustomerDB
CustomerArchiveDB
Both have a Customer table with columns Name, Address, Year etc.
I want to get all the customers from both databases using Entity Framework.
How do I do this?

Create 2 EF models, one for each database. Materialize the objects with ToList or ToArray from each DbContext and then Concat them together in memory.

Related

See generated SQL for LINQ method syntax query? [duplicate]

This question already has answers here:
How to view LINQ Generated SQL statements?
(8 answers)
Get SQL code from an Entity Framework Core IQueryable<T>
(10 answers)
Closed 11 months ago.
I have a fairly simple LINQ query (using method syntax):
_context.Foo.Count(c => c.Bar > 123);
I need to see the SQL that will run against the database. I'm not using SQL Server, so I unfortunately can't use the SQL Server Profiler.
I'm unable to use query syntax in this case.
Is there any sort of .ToQueryString(); method?

"System.NullReferenceException" Error While Using Linq [duplicate]

This question already has answers here:
EF LINQ include multiple and nested entities
(7 answers)
Closed 1 year ago.
I'm trying to learn one to many relations with EF Core.
In summary: I have two tables; Artists and Songs.
But when I try to reach songs which belongs that artist I am getting error.
Why I'm getting null reference error?
You have to include the related entity you want in the list manually, include Songs like the following way:
var theArtist = DB.Artists.Include("Songs").FirstOrDefault(a => a.ID==1);

How to make Microsoft Entity Framework 6.0 bulk update/delete faster? [duplicate]

This question already has answers here:
How to Bulk Update records in Entity Framework?
(9 answers)
Closed 2 years ago.
In my ASP.NET application I am using Entity Framework 6 with SQL Server. As per my scenario I need to perform a bulk update (40k row at a time) and bulk delete operation (40k row at a time) with EF6. EF6's RemoveRange method seems very very slow and update takes forever. Is there any way to speed this up?
you should try a stored procedure to delete the items, stored procedures are much faster and pre-compiled form stored in SQL Server.
for reference to work with Stored Procedure please visit the link.
Stored Procedure with Entity Framework

sqlite EF6 code first not creating tables [duplicate]

This question already has answers here:
Entity Framework 6 with SQLite 3 Code First - Won't create tables
(2 answers)
Closed 8 years ago.
I'm trying to run sqlite in memory to run my integration tests against the database.
But I keep getting error message mentioning the tables are not created by code first.
I did not find any clue mentioning sqlite ef6 nuget package can or can not do this.
If not, what would be other options you recommend for in memory databases.
I would be thankful if you help.
Check the answer at : Entity Framework 6 with SQLite 3 Code First - Won't create tables
Btw, there is a nuget package available for SQLite : Here
To Install package :
Install-Package System.Data.SQLite.EF6

Using linq to select all tables in database [duplicate]

This question already has answers here:
Linq: Get a list of all tables within DataContext
(4 answers)
Closed 8 years ago.
I want to select list of all tables (Not columns) in a database using LINQ dynamically.
I just want it Dynamically not what listed in DataContext static values.For example i alter Table2 after deploying program. in this situation how i should find it.
By the way of there are any query also please let me know.
I think you can use the "Mapping" feature of LINQ:
context.Mapping.GetTables();
if you want to get tables that are modeled you can use #Mygyll answer, but if you want to list all tables in database you can use SMO, in smo when you have a database you can get all tables via this code
db.Tables.Cast<Table>()

Categories