SQL 数据库、实体数据模型和 1 对 1 实体

发布于 2024-12-27 10:53:46 字数 918 浏览 1 评论 0原文

我对 EDM 很陌生,过去写过很多 ADO.Net 的东西。我有三个表:

**Product:**
Prod_ID - PK

**Product_MaxLoan**
Prod_ID - PK

**Product_MinLoan**
Prod_ID - PK

这些表托管在 MS SQL 2005 中,尚未配置 FK 或约束,它们具有名义上的 1 对 1 关系。例如,ID 为 1 的产品的每一行,Product_MaxLoan 和 Product_MinLoan 中都会有一行 ID 为 1。

在 Visual Studio 2010 中,我想正确设置 EDM,以便将基数设置为 1到 1。我之前对表和以下设置进行了 FK 约束,但是,这仅允许 0..1 基数(我想是为了满足产品可能没有Product_MaxLoan 或 Product_MinLoan)。

**Product:**
Prod_ID - PK

**Product_MaxLoan**
ID - PK
Prod_ID - FK

**Product_MinLoan**
ID - PK
Prod_ID - FK

问题

  • 对于在 SQL 2005 中设置这些表,您有何建议?对于 EDM 中的一对一关系,您会设置 FK 吗?
  • 您能否在 SQL 2005 中设置 EDM 在从数据库导入时读取的 PK 关系?
  • 一个产品包含大约 300 个属性,因此包含所有这些 单个表中的数据将是较差的数据库规范化(因此 许多 1 - 1 表)。最佳实践是将所有这些 属性集成到单个 EDM 类中?我的直觉反应是打破它 与数据库中的结构差不多(这是我的 ADO 遗产) 脱颖而出),为每个逻辑部分都有一个类 产品。

您的建议将不胜感激。

最好的问候,

马克

I am quite new to EDMs, having written quite a lot of ADO.Net stuff in the past. I have three tables:

**Product:**
Prod_ID - PK

**Product_MaxLoan**
Prod_ID - PK

**Product_MinLoan**
Prod_ID - PK

These tables, hosted in MS SQL 2005, have no FKs or constraints configured as yet, they are to have a notional 1 to 1 relationship. For example, every row of a Product with ID of 1, there will be a row in Product_MaxLoan and Product_MinLoan each with an ID of 1.

In Visual Studio 2010, I want to set the EDM up correctly so that the cardinality is set to 1 to 1. I previously had FK constraints on the tables and the following set up, however, this would only allow a 0..1 cardinality (to cater, I suppose, for the fact a Product may not have a Product_MaxLoan or Product_MinLoan).

**Product:**
Prod_ID - PK

**Product_MaxLoan**
ID - PK
Prod_ID - FK

**Product_MinLoan**
ID - PK
Prod_ID - FK

Questions:

  • What advice would you give for setting these tables up in SQL 2005? For a 1 to 1 relationship in an EDM would you set up FKs?
  • Can you set up a PK relationship in SQL 2005 that the EDM will read when importing from a database?
  • A product contains some 300 properties, so containing all of this
    data in a single table would be poor database normalization (hence
    the many 1 - 1 tables). Would best practice be to put all of these
    properties into a single EDM class? My gut reaction is to break it
    down much as it is structured in the DB (this is my ADO heritage
    coming to the fore), having a class for each logical part of the
    product.

Your advice would be appreciated.

Best regards,

Mark

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

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

发布评论

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

评论(1

风吹短裙飘 2025-01-03 10:53:46

在数据库中使用此配置:

**Product:**
Prod_ID - PK

**Product_MaxLoan**
Prod_ID - PK, FK (to Product)

**Product_MinLoan**
Prod_ID - PK, FK (to Product)

这将在数据库级别以及 EF 中强制建立一对一关系。关系本身将为 1 - 0..1(Product 可以在没有 MaxLoanMinLoan 的情况下存在),因为真正的 1 : 1 不能存在于数据库中。 Real 1 : 1 要求两个实体始终存在 = 如果第二个实体不存在则无法插入第一个实体,如果第一个实体不存在则无法插入第二个实体。如何在不关闭引用完整性的情况下插入它们?

In database use this configuration:

**Product:**
Prod_ID - PK

**Product_MaxLoan**
Prod_ID - PK, FK (to Product)

**Product_MinLoan**
Prod_ID - PK, FK (to Product)

This will force one-to-one relation on database level as well as in EF. The relation itself will be 1 - 0..1 (Product can exists without MaxLoan and MinLoan) because real 1 : 1 cannot exist in database. Real 1 : 1 demands that both entities always exists = you cannot insert the first if the second doesn't exist and you cannot insert the second if the first doesn't exists. How do you insert them without turning off referential integrity?

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