我可以将实体关联设置为可选或可为空吗?

发布于 2024-11-16 03:02:01 字数 398 浏览 0 评论 0原文

我有一个超级英雄。超级英雄可以拥有很多武器。超级英雄可以拥有很多车辆。 超级英雄可以将武器安装在车辆上,但他们不应该这样做。

http://fm001-forumimages.s3.amazonaws.com/EntityHero.png

在实体框架中,如果我创建武器并将其分配给超级英雄,它还会强制我将其附加到车辆上(下面的错误)。

  1. 这是预期的行为吗?
  2. 超级英雄、武器和载具之间正确的实体关联映射是什么?
  3. 有什么好的 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).

  1. Is this the expected behavior?
  2. What's the correct Entity Association mapping between Superhero, Weapon, and Vehicle?
  3. What's a good sample Google search phrase to understand this better?

Thanks!

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

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

发布评论

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

评论(2

_蜘蛛 2024-11-23 03:02:01

您需要武器与超级英雄以及武器与车辆之间的多对多关系。它可能会表示为一对链接表,如下所示:

CREATE TABLE SuperheroWeapons (
    SuperheroId bigint NOT NULL,
    WeaponId bigint NOT NULL,
    CONSTRAINT SuperheroWeapons_PK PRIMARY KEY CLUSTERED (SuperheroId ASC, WeaponId ASC),
    CONSTRAINT SuperheroWeapons_FK_SuperheroId FOREIGN KEY (SuperheroId) REFERENCES Superheros (SuperheroId),
    CONSTRAINT SuperheroWeapons_FK_WeaponId FOREIGN KEY (WeaponId) REFERENCES Weapons (WeaponId)
)

与 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:

CREATE TABLE SuperheroWeapons (
    SuperheroId bigint NOT NULL,
    WeaponId bigint NOT NULL,
    CONSTRAINT SuperheroWeapons_PK PRIMARY KEY CLUSTERED (SuperheroId ASC, WeaponId ASC),
    CONSTRAINT SuperheroWeapons_FK_SuperheroId FOREIGN KEY (SuperheroId) REFERENCES Superheros (SuperheroId),
    CONSTRAINT SuperheroWeapons_FK_WeaponId FOREIGN KEY (WeaponId) REFERENCES Weapons (WeaponId)
)

and similar for the VehicleWeapons table.

无风消散 2024-11-23 03:02:01

武器和车辆之间的实体框架关系以及超级英雄和武器之间的关系都已关闭。

您需要使其成为多对多关系,即。有许多载具有许多武器,也有许多武器属于许多载具。

您可以使用映射到武器和车辆以及超级英雄和武器的中间实体来完成此任务。
看看 与 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

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