一步一步体验 ADO.NET Entity Framework:第3步:理清思路——EDM中的关系和继承
为了让我们对定义EDM有一个清晰的思路,这一篇文章我们来体验一下ADO.NET Entity Framework中的关系和继承,
关系:在我们的现实生活中关系随处可见,父子关系、主从关系等,在数据驱动的应用系统中,我们更是对关系有了更为深入的认识和研究,那么在我们熟悉的关系型数据应用系统中,关系的类型也不过如下几种:
.一对一的关系
.一对多的关系
.多对多的关系
.其他松散的关系
那么在EDM中这些关系是如何定义的呢?我们来看看以上关系的EDM实现:
.一对一关系:
比如我们假定要给单位实体和货品实体定义一对一的关系,如图:
EDM代码如下:
一对一关系代码:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

一对多关系的代码:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

多对多关系代码:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

关于关系的更多详细信息,参看msdn(英文)
http://msdn.microsoft.com/en-us/library/bb399566.aspx
http://msdn.microsoft.com/en-us/library/bb399277.aspx
可以帮助理解。
继承:我们来看看ms对继承在EDM是怎么描述的:
In the Entity Data Model (EDM), inheritance permits a derived type to extend the features of another type.
Typically, the model used by an application includes many different types. Some entity types model separate concepts, such as customer and order in a business application. These data types do not share any members, but there are other types that are more similar to each other. For example, consider the customer and employee types. Although these types model different concepts, there is underlying commonality: Both represent people involved in business relationships and both include properties to store information such as name, address, and telephone number.
EDM inheritance permits one type to be derived from another type. For example, Employee and Customer can both inherit from the Contact type. In this case, Contact is called the base type. Employee and Customer are called derived types.
Inheritance does not have to stop at a single level: It is possible for a derived type to be the base type of another entity type. For example, Employee is a base type of Manager; Customer is a base type of PreferredCustomer, and so on. In this manner, inheritance hierarchies are built.
Like the common language runtime (CLR), the EDM system supports only single inheritance of data types. An entity type can only directly inherit properties from one super-type.
这一段Like the common language runtime (CLR), the EDM system supports only single inheritance of data types. An entity type can only directly inherit properties from one super-type. 值得注意哦。
我们来看看实现的效果如图:
其实就是一个从基实体派生的过程,代码如下:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

MSDN的参考信息连接如下:
http://msdn.microsoft.com/en-us/library/bb399159.aspx
http://msdn.microsoft.com/en-us/library/cc716744.aspx
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构