ADO.Net EF - 如何定义模型第一种方法外键关系? [英] ADO.Net EF - how to define foreign key relation in model first approach?

查看:130
本文介绍了ADO.Net EF - 如何定义模型第一种方法外键关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直有previous问题,关于类的继承和构建解决这个数据库,并使用实体框架没有成功。所以我试图创建在Visual Studio中的实体,看看数据库表会自动创建。

I have been having previous issues regards to class inheritance and structuring a database around this and using the entity framework to no success. So I have tried to create the entities inside visual studio and see what database tables it creates automatically.

我有一个实体MediaItem这是抽象的,游戏从这个继承。游戏中有一个控制台(智力)相当于ConsoleID。然而,当我生成数据库我得到一个额外的不必要的列(ConsoleTypes_ConsoleID)MediaItems_Game表内。这是为什么,我该如何prevent这种情况的发生?谢谢你。

I have a Entity MediaItem which is Abstract and Game Inherits from this. Game has a Console (Int) which corresponds to ConsoleID. However, when I generate the database I get an extra unwanted column (ConsoleTypes_ConsoleID) inside MediaItems_Game table. Why is this and how can I prevent this from happening? Thanks.

推荐答案

首先,你的模型可能是错误的。 ConsoleType 游戏是不是在一到一个关系(除非你为每个主机类型的单机游戏)。我预计1控制台可以有多个游戏。因此它应该是对多。在现实游戏可以释放在多个平台所以应该多到许多。

First of all your model is probably wrong. ConsoleType and Game is not in one-to-one relation (unless you have single game for each console type). I expect 1 console can have multiple games. So it should be one-to-many. In reality game can be released on multiple platforms so it should be many-to-many.

您有unvanted列,因为之间的关系 ConsoleType 游戏不知道它应该使用控制台属性作为一个外键。这种情况如果您使用独立相关。独立协会所使用的默认情况下,当你从一个实体吸引他们到实体设计等实体。您必须使用外键关联。

You got unvanted column because your relation between ConsoleType and Game doesn't know that it should use Console property as a foreign key. This happens if you use independent association. Independent associations are used by default when you draw them from one entity to other entity in Entity designer. You must use foreign key association.

开始用此设置(吸取 ConsoleType 协会游戏 - 你必须有一个一对多关系):

Start with this set up (draw association from ConsoleType to Game - you must have one-to-many relation):

之间的选择关系 ConsoleType 游戏的在属性中单击上的引用约束的:

Select relation between ConsoleType and Game an in properties click on Referential Constraint:

引用约束的对话框,刚刚成立的关系:

In Referential Constraint dialog just set up relation:

保存模型,然后重新生成数据库。

Save your model and generate database again.

这篇关于ADO.Net EF - 如何定义模型第一种方法外键关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆