如何准备数据库以从中生成 EF 模型

发布于 2024-11-13 22:09:05 字数 637 浏览 1 评论 0原文

我对如何在关系数据库结构中表示我的对象有一些误解。

例如,如果我有一个简单的表结构,如下所示:

在此处输入图像描述

如果我生成实体框架模型基于此,它看起来像:

在此处输入图像描述

正如您所看到的,它是多对多关联。 (任何菜肴都包含许多配料,任何配料都可以在许多菜肴中使用)

现在,如果我需要一些额外的参数,例如菜肴中配料的数量,该怎么办? (当然,任何不同菜肴中的每种成分通常都有不同的编号)。

如果我直接向 Dish_FooIngridient 表添加一些附加列,则会破坏自动模型生成的准确性,然后我必须在 EF 模型设计器中手动修复关联,然后使用一些繁琐的查询来操作对象。因为它会生成成这样的东西:

enter image description here

正如你所看到的,现在还有其他多余的属性,我不需要。有没有更好的方法来管理它?也许使用复杂的属性或继承的实体,或者其他什么?

I have some misunderstanding how my objects should be represented in a relational db structure.

For example if I have a simple tables structure that looks like that:

enter image description here

And if I generate an Entity Framework model based on that, it would be look like:

enter image description here

As you can see it's many-to-many association. (Any Dish contains many Ingredients, any Ingredient can be used in many Dishes)

Now, what if I need some additional parameters, like quantity of the Ingredient in a Dish? (Of course it usually would be distinct number for each ingredient in any different Dish).

If I add some additional columns directly to Dish_FooIngridient table, it breaks nicety of automatic model generation, and then I have to fix associations manually in EF model designer and later use some cumbersome queries to operate with objects. Because it will be generated into something like this:

enter image description here

As you can see, now there are other redundant properties, which I don't need. Is there any better way to manage that? Maybe using complex properties or inherited entity, or something else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青瓷清茶倾城歌 2024-11-20 22:09:05

正如 Apress 的“Entity Framework 4.0 Recipes”中所建议的,设计没有负载的多对多关系并不是一个很好的实践。

不幸的是,一个以多个、无负载、多对多关系开始的项目通常会以多个、负载丰富、多对多关系结束。重构模型(尤其是在开发周期的后期)以适应多对多关系中的有效负载可能会很乏味。不仅引入了额外的实体,而且通过关系的查询和导航模式也发生了变化。一些开发人员认为,每个多对多关系都应该从一些有效负载(通常是合成密钥)开始,因此不可避免地添加更多有效负载对项目的影响要小得多。所以这是最佳实践。如果您有一个无负载的多对多关系,并且您认为它可能会随着时间的推移而改变以包含负载,请从一个额外的身份列开始
链接表。当您将表导入到模型中时,您将获得两个一对多关系,这意味着您编写的代码和您拥有的模型将准备好应对随着项目成熟而出现的任意数量的附加有效负载列。额外的整数标识列的成本通常是为了保持模型更多而付出的相当小的代价
灵活。

As it's suggested in Apress' "Entity Framework 4.0 Recipes", designing Many-To-Many Relationships without a payload not a very good practice.

Unfortunately, a project that starts with several, payload-free, many-to-many relationships often ends up with several, payload-rich, many-to-many relationships. Refactoring a model, especially late in the development cycle, to accommodate payloads in the many-to-many relationships can be tedious. Not only are additional entities introduced, but the queries and navigation patterns through the relationships change as well. Some developers argue that every many-to-many relationship should start off with some payload, typically a synthetic key, so the inevitable addition of more payload has significantly less impact on the project. So here’s the best practice. If you have a payload-free, many-to-many relationship and you think there is some chance that it may change over time to include a payload, start with an extra identity column in
the link table. When you import the tables into your model, you will get two one-to-many relationships, which means the code you write and the model you have will be ready for any number of additional payload columns that come along as the project matures. The cost of an additional integer identity column is usually a pretty small price to pay to keep the model more
flexible.

∞琼窗梦回ˉ 2024-11-20 22:09:05

我不明白为什么。将字段添加到 Dish_FoodIngredient 并为其指定主键。将该表添加到您的模型中即可。您可能需要在代码中进行一些重构,但不要为此责怪 EF。

I don't see why. Add the fields to Dish_FoodIngredient and give it a primary key. Add that table to your model and thats' it. You need to do some refactoring in your code probably but don't blame EF for that.

眼眸里的快感 2024-11-20 22:09:05

双击 Model.edmx 文件,然后在对象旁边的空白处右键单击并单击“从数据库更新模型...”,然后单击“完成”,它将刷新您的表。

Double-click your Model.edmx file, then in the white space next to your objects, right-click and click "Update Model from Database...", then click Finish, and it'll refresh your tables.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文