Joining multiple tables from different databases - entity-framework

I am working on ASP.NET core application where I am using Entity Framework to interact with SQL server database. so far, I was using Scaffold-DbContext command to create new model from SQL server database. Even if , we wanted to create DbContext based on multiple tables, I could do that using -t flag in above command. Everything happening on single SQL database.
Scaffold-DbContext "Server=XXXXXXXXX;Database=XXXXXXXX;User Id=XXXXX;Password:XXXXXX" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -t Table1 Table2
From Scaffold-DbContext parameters commands, I couldn't find option to specify different databases.
In new scenario, I have to join 2 tables from different SQL databases. Is there a way to create model which is consist of 2 tables from different databases. Both tables are using one-to-one relationship between them.
For example- DB1 has table1 and DB2 has table2. Is there a way to create a DbContext which is consist of these 2 tables (table1 and table2)?
Is there any other way to achieve join between 2 tables from 2 different database?

No, you can't do that with EntityFramework (neither 6.x nor Core). A DbContext is per database and you can only do joins within the same DbContext.
You could create a view and map the view to the models you needs, but iirc. mapping of views is still on the roadmap for EntityFramework Core. May work with EntityFramework 6.x though.
Edit:
At least you can't doing it with Linq/Fluent api. You can execute raw queries though. The catch: The project must match the model exactly, there can't be any missing fields of the model. The Ad-hoc mapping to non-entities is on the roadmap for future versions of EntityFramework Core

Related

Entity Framework Core configurations with existing database

In one of my projects, I am using an existing SQL Server database. All the database scripts are managed using DBUp and SQL script migrations.
In my application, I am using Entity Framework Core to communicate with this database. When I configure my entities in EF configurations, should I still define functions like IsRequired(), HasMaxLenth() etc.?
I am not using these EF configurations to generate migration scripts; all the migration is outside of EF. I am just using these configurations to communicate with the database.
When I configure my Entities in EF configurations, should I still define functions like IsRequired(), HasMaxLenth() etc.?
Other than table and column name mapping and data type mapping, it's not required, but additional model metadata might be used by front-end components for validation.
In general, yes, you should keep them. Many of these configurations are used throughout EF to make decisions at runtime. For example, some queries can be further optimized if EF knows that a column is never NULL, the max length is used to configure the SQL parameters it sends to the database, and unique constraints are used to sort SQL statements during SaveChanges.
While a few things like constraint names, non-unique indexes, index filters, and sequences aren't currently used at runtime, it's hard to know which ones EF will and won't use, so it's best just to keep them all.
And sometimes, database features like always encrypted on SQL Server, will fail entirely if the mappings aren't precise.

How to create DB Context run time?

In my asp.net core application, each client has their own database schema.
Hence there are many databases. Sometimes the tables and fields will be different.
I have used Entity Framework core as ORM and we are following Generic Repository pattern.
Is there any option to generate DB Context at run time corresponding to each client?
Thanks
Krishnan

What SQL Server 2017 features are not supported in Entity Framework Core code first?

Our team is thinking of utilizing Entity Framework Core code-first to help model the database. We can have both DB projects and EF models, as per article here Database Projects vs. Entity Framework Database Migrations utilizing schema compares, just trying to figure out what will be the source of truth?
Does Entity Framework support all features in SQL Server SSDT Database Projects?
What features does EF Core 2 not support? (eg, does it not support any of following: triggers, views, functions, stored procedures, encryption keys, certificates, db properties (ansi null, quoted identifier), partitions)
I am trying to locate the Microsoft Resource.
tl;dr Database Projects are feature-rich, but database-first. Migrations is code-first, but has a very limited built-in set of database features.
For many people it won't be relevant to compare Database Projects and Migrations. They represent two different modes of working with Entity Framework. Migrations is code-first, DP is database-first. Sure, you can use migrations to control the database schema and besides that keep a DP in sync with the generated database to satisfy DBAs (as the link suggests). But both lead their own separate lives and there's no Single Source Of Truth.
So comparing them is useful if you're not sure yet wich working mode you're going to choose.
For me the most important difference is that DP will cover all database objects and detect all changes between them when comparing databases. Migrations only detect changes between a database and the mapped model. And the set of options for generating database objects is very limited. For everything you need additionally you have to inject SQL statements into the migration code. These statements are your own responsibility. You have to figure out yourself if a migration needs an ALTER PROCEDURE statement or not (for example). EF won't complain if the script and the database differ in this respect.
This is the main reason why I've never been a great fan of migrations. It's virtually impossible to maintain a mature database schema including storage, file groups, privileges, collations, and what have you.
Another advantage of DP is that they're great in combination with source control. Each database object has its own file and it's very easy to check the change history of each individual object. That's not possible with generated migrations. Indeed, many intermediate changes may never make it to a generated migration.
Of course the obvious advantage of migrations is the possibility to do a runtime check (albeit incomplete) whether the code and the database match. In database-first projects you need to create your own mechanism for that.
EF Core is only ORM.
1) You should be ready to create all DB objects except tables manually. What I create manually: constrates (defaults as well as conditions). Since this is code first - there is no need in SP, functions and so on. If you use ORM - DB is only storage. Of course practice is important. For me default constraints adds comfort on tables where I create test data manually. And conditions also are usefull in situations when you do not trust your (team) code.
2) you will do creation (and dropping) of views, triggers, sp and so on to the "migration" code (there is such concept in EF) in plain sql:
migrationBuilder.Sql("CREATE VIEW ...");
As a result you could have a separate "migration" program (e.g. command line tool) that install or remove both Ef Core tables and your manually created objects, do and revert the data migrations.
"EF Core migrations" is quite complex api (reserve a week for learning). Interesting topics: managing several dbcontexts in one db, createing db object during migration from model annotations, unistall. Or find a freelancer for it (this part of project is good for outsourcing).

.net core entity framework create tables inside existing database

I don't want to give my sql user the permissions to create databases.
Is there any way to create the database manually and then have entity framework create the tables inside it?
I'm certain EF Core is smart enough to handle this case. Just create the database, set your permissions and run the command dotnet ef update-database (assuming you have a valid migration).
Usually, an application (or multiple applications) use the same database from separate DbContext classes, which handle their own bounded context (a logical piece of the whole). That would require being able to recognize that Databases and Tables have already been created, and issue appropriate add and alter commands to the schema.

one or more Entity models for one database for entity framework?

When use entity framework for DAL tier, VS 2010 can create edmx for each database.
Question:
If I have a database with many tables, should I create only one edmx for all tables or mutiple edmx files? for example, maybe all security tables for one edmx file, other tables for another edmx file. If there is more than one, then in other tiers, there will have more then on ObjectContext in code for business logic.
Which one it the best solution for this case?
I've done this before when experimenting with AdventureWorks. If you have a large database with lots of tables, and the tables are segmented into separate schemas (like the Purchasing, Sales, HumanResources schemas in AdventureWorks) then it may work well to create multiple models. They don't have to be separate schemas- any two groups of related tables where there are no relations between the groups would work.
You'd want to make sure that you include all related tables in each model so that you don't have to try to join entities across models.