如何准备数据库以从中生成 EF 模型
我对如何在关系数据库结构中表示我的对象有一些误解。
例如,如果我有一个简单的表结构,如下所示:
如果我生成实体框架模型基于此,它看起来像:
正如您所看到的,它是多对多关联。 (任何菜肴都包含许多配料,任何配料都可以在许多菜肴中使用)
现在,如果我需要一些额外的参数,例如菜肴中配料的数量,该怎么办? (当然,任何不同菜肴中的每种成分通常都有不同的编号)。
如果我直接向 Dish_FooIngridient
表添加一些附加列,则会破坏自动模型生成的准确性,然后我必须在 EF 模型设计器中手动修复关联,然后使用一些繁琐的查询来操作对象。因为它会生成成这样的东西:
正如你所看到的,现在还有其他多余的属性,我不需要。有没有更好的方法来管理它?也许使用复杂的属性或继承的实体,或者其他什么?
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:
And if I generate an Entity Framework model based on that, it would be look like:
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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 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.
我不明白为什么。将字段添加到 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.
双击 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.