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

查看:266
本文介绍了在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表示必须在right后面插入left,但是必须在left =不可能之后插入right.因此,使用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天全站免登陆