Is a EF Core model represented as part of a C# .NET Core program?

c# entity-framework-core

Question

https://docs.microsoft.com/en-us/ef/core/modeling/ says

Entity Framework uses a set of conventions to build a model based on the shape of your entity classes. You can specify additional configuration to supplement and/or override what was discovered by convention.

and https://docs.microsoft.com/en-us/ef/core/managing-schemas/ says

EF Core provides two primary ways of keeping your EF Core model and database schema in sync. To choose between the two, decide whether your EF Core model or the database schema is the source of truth.

If you want your EF Core model to be the source of truth, use Migrations. As you make changes to your EF Core model, this approach incrementally applies the corresponding schema changes to your database so that it remains compatible with your EF Core model.

Use Reverse Engineering if you want your database schema to be the source of truth. This approach allows you to scaffold a DbContext and the entity type classes by reverse engineering your database schema into an EF Core model.

I originally thought that an EF Core model is some entity classes in C#. But the above two quotes seem to suggest an EF Core model is something sitting between the entity classes and the database.

So I was wondering whether a EF Core model is represented as part of a C# .NET Core program? I don't have Visual Studio IDE, and I can only sense its existence as some part of the program.

1
-1
12/30/2019 6:14:45 PM

Accepted Answer

But the above two quotes seem to suggest an EF Core model is something sitting between the entity classes and the database.

Yes - and no.

Internally, Entity Framework (both EF6 and EF Core) have a "database model" which is internal to EF and programmers have no access to (unless they poke around the internals). This model is not represented by POCO or "entity objects" but by an internal system which we (as consumers of EF) don't interact with (so it doesn't matter).

In older versions of Entity Framework this was the *.edmx XML file which lists all of the database tables, columns, sprocs, UDFs, etc and maps them to .NET class types and their members. This is the internal database model it's referring to.

Recent versions of EF (including EF Core) do-away with this EDMX file. The internal database model still exists, it's just generated at runtime (using the Configuration code) instead of using the *.edmx files).

For more details, I recommend this StackOverflow QA from 2012 that's referring to Entity Framework 4's 3 different "modes" (Code-first, Database-first, and Model-first). EF Core is now technically only Code-first, but "Database-first" is accomplished by regenerating the POCO and Configuration code from a real-life SQL Server database (or other supported RDBMS).

1
12/30/2019 6:31:10 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow