最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Database normalization using EF Core - Stack Overflow

programmeradmin1浏览0评论

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
  • "It contains tables representing relationships between two other tables" — please provide an example. We shouldn't guess what your mentor has suggested. – Svyatoslav Danyliv Commented Mar 13 at 10:36
  • 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 to Blog and Post entities and use the many-to-many table to fill the Blog.Posts property – Panagiotis Kanavos Commented Mar 13 at 10:39
  • I 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:43
  • "using EF Core, how can databases be normalized?" - Kicker: That partly depends on EF Core Version in use! – Fildor Commented Mar 13 at 10:44
  • 1 BTW if you have Person.Orders and Order.Persons collection properties in your code, EF Core will automatically map that relation to a PersonOrders table without creating a bridge entity – Panagiotis Kanavos Commented Mar 13 at 10:50
 |  Show 1 more comment

1 Answer 1

Reset to default 5

The "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

发布评论

评论列表(0)

  1. 暂无评论