在 SQL Server 中实现一对零或一关系 [英] Implementing one-to-zero-or-one relation in SQL Server

查看:35
本文介绍了在 SQL Server 中实现一对零或一关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Entity Framework 4.1 数据库优先方法.我使用过旧数据库.在我根据旧数据库中的表创建实体类的 edmx 文件中,某些实体之间存在一对零或一关联.

I'm using Entity Framework 4.1 database first approach. I've used legacy database. In my edmx file which created entity classes based on tables in the legacy database, there is a one-to-zero-or-one association between some entities.

虽然我探索了数据库的表和它们之间的关系,但我没有发现一对零或一关系是如何在数据库中实现的.

Although I explored the tables of database and relation between them I didn't find out how one-to-zero-or-one relation have been implemented in database.

有关更多信息,我在 edmx 文件中放了一些我的数据库图表及其关系和通信实体的属性的屏幕截图:

For more information I put some screenshots of my database diagram and the property of its relation and correspondent entities in edmx file:

推荐答案

数据库中的 1-0..1 关系是直接可见的.它建立在 Course 和 OnlineCourse 表之间,其中 Course 是关系 (1) 的主体,而 OnlineCourse 依赖于在 CourseID 上配置的 FK.FK 也是 OnlineCourse 的 PK = 它必须是唯一的,因此它是 0..1.

The 1-0..1 relation in your database is directly visible. It is built between Course and OnlineCourse tables where Course is principal in relation (1) and OnlineCourse is dependent with FK configured on CourseID. FK is also PK of the OnlineCourse = it must be unique and because of that it is 0..1.

数据库总是"使用 1 - 0..1 因为实数 1 - 1 不能有效地用于数据插入.1 - 1 表示必须在右后插入左,但必须在左后插入右=不可能.因为 1 - 0..1 用在 left 是主体的地方,必须在 right 之前插入,right 是从属的,必须在 left 之后插入.

Database "always" uses 1 - 0..1 because real 1 - 1 cannot be effectively used for data insertion. 1 - 1 means that left must be inserted after right but right must be inserted after left = impossible. Because of that 1 - 0..1 is used where left is principal and must be inserted before right and right is dependent and must be inserted after left.

这篇关于在 SQL Server 中实现一对零或一关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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