I have a project built using Entity Framework Core and using a code-first approach. Recently, my mentor proposed a new normalized database structure for the project, so I have to rewrite everything.
It contains tables of the relationship between two other tables. I don't quite understand how this can be done using EF Core. To be honest, I don't really like storing an entire model in the code that represents the relationships between the models.
My question is: using EF Core, how can databases be normalized?
Edit: I have laid out the scheme >> . about the table that "contains tables representing relationships between two other tables", I meant the one marked in red.
I have a project built using Entity Framework Core and using a code-first approach. Recently, my mentor proposed a new normalized database structure for the project, so I have to rewrite everything.
It contains tables of the relationship between two other tables. I don't quite understand how this can be done using EF Core. To be honest, I don't really like storing an entire model in the code that represents the relationships between the models.
My question is: using EF Core, how can databases be normalized?
Edit: I have laid out the scheme >> https://imgur/a/aNcuexU. about the table that "contains tables representing relationships between two other tables", I meant the one marked in red.
Share Improve this question edited Mar 13 at 10:46 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 13 at 10:28 RedplcsRedplcs 3182 silver badges8 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 5The "tables of the relationship between two other tables" is a Many-To-Many Relationship. This can be achieved by giving both tables an ICollection List of the other Class. EF Core will do the rest and generate it.
https://learn.microsoft/en-us/ef/core/modeling/relationships/many-to-many more on this under the chapter Basic many-to-many
Now to your disliking of storing an entire model of the DB in code. Why exactly don't you want to do it? Is there any specific reason?
Edit: since the scheme got provided. EF will automatically map the needed Table as seen in the scheme so EF does it just right
I don't quite understand how this can be done using EF Core.
it's already done. EF is an ORM, not a database driver or database model. It already maps many-to-many relations to entities and properties. Entities aren't tables. If you have Blogs, Posts and BlogPosts tables, EF Core 7 and later will map them toBlog
andPost
entities and use the many-to-many table to fill theBlog.Posts
property – Panagiotis Kanavos Commented Mar 13 at 10:39I don't really like storing an entire model in the code
you aren't. Those are your application's classes, not representations of the tables. ORMs like EF map those classes to tables using "magic", trying to give the impression of working with an object database. When they load data, they load entire graphs of related objects at once. If you ask for an Author's blogs and posts, EF will load all the related objects in a single operation. A DbContext is a Unit-of-Work and only needs to specify the entities needed for a specific domain. Not all tables – Panagiotis Kanavos Commented Mar 13 at 10:43Person.Orders
andOrder.Persons
collection properties in your code, EF Core will automatically map that relation to aPersonOrders
table without creating a bridge entity – Panagiotis Kanavos Commented Mar 13 at 10:50