我可以将实体关联设置为可选或可为空吗?
我有一个超级英雄。超级英雄可以拥有很多武器。超级英雄可以拥有很多车辆。 超级英雄可以将武器安装在车辆上,但他们不应该这样做。
http://fm001-forumimages.s3.amazonaws.com/EntityHero.png
在实体框架中,如果我创建武器并将其分配给超级英雄,它还会强制我将其附加到车辆上(下面的错误)。
- 这是预期的行为吗?
- 超级英雄、武器和载具之间正确的实体关联映射是什么?
- 有什么好的 Google 搜索短语示例可以帮助您更好地理解这一点?
谢谢!
I have a Superhero. Superheros can have many weapons. Superheros can have many vehicles.
Superheros can attach their weapons to vehicles, but they shouldn't have to.
http://fm001-forumimages.s3.amazonaws.com/EntityHero.png
In the Entity Framework, if I create a weapon and assign it to a Superhero, it also forces me to attach it to a vehicle (error below).
- Is this the expected behavior?
- What's the correct Entity Association mapping between Superhero, Weapon, and Vehicle?
- What's a good sample Google search phrase to understand this better?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要武器与超级英雄以及武器与车辆之间的多对多关系。它可能会表示为一对链接表,如下所示:
与 VehicleWeapons 表类似。
You need a many to many relation on the weapons to superheroes and weapons to vehicles. It'd be likely expressed as a pair of linking tables, like this:
and similar for the VehicleWeapons table.
武器和车辆之间的实体框架关系以及超级英雄和武器之间的关系都已关闭。
您需要使其成为多对多关系,即。有许多载具有许多武器,也有许多武器属于许多载具。
您可以使用映射到武器和车辆以及超级英雄和武器的中间实体来完成此任务。
看看 与 EF 的多对多关系
Your Entity framework relationship between Weapons and Vehicles is off as well as superheros and weapons.
You'll want to make it a many to many relationship, ie. There are many vehicles with many weapons, and there also are many weapons that belong to many vehicles.
You can accomplish this with an intermediary entity one that maps to both weapons and vehicles and superhero and weapons.
take a look at Many to Many Relationships with EF