admin管理员组文章数量:1422955
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
本文标签: cDatabase normalization using EF CoreStack Overflow
版权声明:本文标题:c# - Database normalization using EF Core - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744707709a2620939.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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