edm 单向一对一关系

it2022-05-05  147

刚刚接触微软的Entity Framework,还有很多东西都不懂,在网上查询发现中文博客很少,所以只能看官方的文档

SSDL Specification,CSDL Specification,MSL Specification

 

记录一下edm外键关系的单向一对一关系。

使用Entity Data Model Designer设计模型如下:

说明一下,BodyId是外键,指向MessageBody的主键Id。看起来是多对一的关系,其实只要BodyId是唯一键就可以

理解为一对一了。真正的一对一是主键作为外键关联主键,比如body的Id既是自己的主键,又是关联message的外键。

 

以上的模型,不管什么first模式(x-first)最终都会生成edm。如下图,

1 <?xml version="1.0" encoding="utf-8"?> 2 <edmx:Edmx Version="3.0" xmlns:edmx="http://schemas.microsoft.com/ado/2009/11/edmx"> 3 <!-- EF Runtime content --> 4 <edmx:Runtime> 5 <!-- SSDL content --> 6 <edmx:StorageModels> 7 <Schema Namespace="Model1.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2012" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl"> 8 <EntityContainer Name="Model1StoreContainer"> 9 <EntitySet Name="MessageSet" EntityType="Model1.Store.MessageSet" store:Type="Tables" Schema="dbo" /> 10 <EntitySet Name="MessageBodySet" EntityType="Model1.Store.MessageBodySet" store:Type="Tables" Schema="dbo" /> 11 <AssociationSet Name="MessageMessageBody" Association="Model1.Store.MessageMessageBody"> 12 <End Role="Message" EntitySet="MessageSet" /> 13 <End Role="MessageBody" EntitySet="MessageBodySet" /> 14 </AssociationSet> 15 </EntityContainer> 16 <EntityType Name="MessageSet"> 17 <Key> 18 <PropertyRef Name="Id" /> 19 </Key> 20 <Property Name="Id" Type="int" StoreGeneratedPattern="Identity" Nullable="false" /> 21 <Property Name="BodyId" Type="int" Nullable="false" /> 22 </EntityType> 23 <EntityType Name="MessageBodySet"> 24 <Key> 25 <PropertyRef Name="Id" /> 26 </Key> 27 <Property Name="Id" Type="int" StoreGeneratedPattern="Identity" Nullable="false" /> 28 </EntityType> 29 <Association Name="MessageMessageBody"> 30 <End Role="Message" Type="Model1.Store.MessageSet" Multiplicity="*" /> 31 <End Role="MessageBody" Type="Model1.Store.MessageBodySet" Multiplicity="1" /> 32 <ReferentialConstraint> 33 <Principal Role="MessageBody"> 34 <PropertyRef Name="Id" /> 35 </Principal> 36 <Dependent Role="Message"> 37 <PropertyRef Name="BodyId" /> 38 </Dependent> 39 </ReferentialConstraint> 40 </Association> 41 </Schema> 42 </edmx:StorageModels> 43 <!-- CSDL content --> 44 <edmx:ConceptualModels> 45 <Schema xmlns="http://schemas.microsoft.com/ado/2009/11/edm" xmlns:cg="http://schemas.microsoft.com/ado/2006/04/codegeneration" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" Namespace="Model1" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" annotation:UseStrongSpatialTypes="false"> 46 <EntityContainer Name="Model1Container" annotation:LazyLoadingEnabled="true"> 47 <EntitySet Name="MessageSet" EntityType="Model1.Message" /> 48 <EntitySet Name="MessageBodySet" EntityType="Model1.MessageBody" /> 49 <AssociationSet Name="MessageMessageBody" Association="Model1.MessageMessageBody"> 50 <End Role="Message" EntitySet="MessageSet" /> 51 <End Role="MessageBody" EntitySet="MessageBodySet" /> 52 </AssociationSet> 53 </EntityContainer> 54 <EntityType Name="Message"> 55 <Key> 56 <PropertyRef Name="Id" /> 57 </Key> 58 <Property Name="Id" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" /> 59 <Property Name="BodyId" Type="Int32" Nullable="false" /> 60 <NavigationProperty Name="MessageBody" Relationship="Model1.MessageMessageBody" FromRole="Message" ToRole="MessageBody" /> 61 </EntityType> 62 <EntityType Name="MessageBody"> 63 <Key> 64 <PropertyRef Name="Id" /> 65 </Key> 66 <Property Name="Id" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" /> 67 </EntityType> 68 <Association Name="MessageMessageBody"> 69 <End Type="Model1.Message" Role="Message" Multiplicity="*" /> 70 <End Type="Model1.MessageBody" Role="MessageBody" Multiplicity="1" /> 71 <ReferentialConstraint> 72 <Principal Role="MessageBody"> 73 <PropertyRef Name="Id" /> 74 </Principal> 75 <Dependent Role="Message"> 76 <PropertyRef Name="BodyId" /> 77 </Dependent> 78 </ReferentialConstraint> 79 </Association> 80 </Schema> 81 </edmx:ConceptualModels> 82 <!-- C-S mapping content --> 83 <edmx:Mappings> 84 <Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2009/11/mapping/cs"> 85 <EntityContainerMapping StorageEntityContainer="Model1StoreContainer" CdmEntityContainer="Model1Container"> 86 <EntitySetMapping Name="MessageSet"> 87 <EntityTypeMapping TypeName="IsTypeOf(Model1.Message)"> 88 <MappingFragment StoreEntitySet="MessageSet"> 89 <ScalarProperty Name="Id" ColumnName="Id" /> 90 <ScalarProperty Name="BodyId" ColumnName="BodyId" /> 91 </MappingFragment> 92 </EntityTypeMapping> 93 </EntitySetMapping> 94 <EntitySetMapping Name="MessageBodySet"> 95 <EntityTypeMapping TypeName="IsTypeOf(Model1.MessageBody)"> 96 <MappingFragment StoreEntitySet="MessageBodySet"> 97 <ScalarProperty Name="Id" ColumnName="Id" /> 98 </MappingFragment> 99 </EntityTypeMapping> 100 </EntitySetMapping> 101 </EntityContainerMapping> 102 </Mapping> 103 </edmx:Mappings> 104 </edmx:Runtime> 105 <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) --> 106 <edmx:Designer xmlns="http://schemas.microsoft.com/ado/2009/11/edmx"> 107 <edmx:Connection> 108 <DesignerInfoPropertySet> 109 <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" /> 110 </DesignerInfoPropertySet> 111 </edmx:Connection> 112 <edmx:Options> 113 <DesignerInfoPropertySet> 114 <DesignerProperty Name="ValidateOnBuild" Value="true" /> 115 <DesignerProperty Name="EnablePluralization" Value="False" /> 116 <DesignerProperty Name="CodeGenerationStrategy" Value="" /> 117 <DesignerProperty Name="UseLegacyProvider" Value="False" /> 118 </DesignerInfoPropertySet> 119 </edmx:Options> 120 <!-- Diagram content (shape and connector positions) --> 121 <edmx:Diagrams> 122 </edmx:Diagrams> 123 </edmx:Designer> 124 </edmx:Edmx>

数据库最终效果图

最终生成的代码:

1 namespace ConsoleApplication3 2 { 3 using System; 4 using System.Collections.Generic; 5 6 public partial class MessageBody 7 { 8 public int Id { get; set; } 9 } 10 11 public partial class Message 12 { 13 public int Id { get; set; } 14 public int BodyId { get; set; } 15 16 public virtual MessageBody MessageBody { get; set; } 17 } 18 }

 

SSDL中的29~40行,定义了存储模型中的关系,可以看到是1-N

CSDL中的68~79行,定义了概念模型中的关系,可以看到也是1-N

看到这里也许会有疑问,既然存储模型中和概念模型中的关系都是1-N,为什么生成的代码(Message中的MessageBody)

却不是ICollection<MessageBody>集合?

答案是CSDL中的第60行。

 

转载于:https://www.cnblogs.com/kitsune/p/6292523.html

相关资源:各显卡算力对照表!

最新回复(0)