ADO.NET实体框架Entity Framework模型-基于XML解析

        最近由于项目需求,需要对实体框架内表之间的关系进行处理,主要功能要求是通过一表名,返回其在实体框架内的所有关系表、主外键及每个字段的属性。先简单描述我解决这个问题从开始到最后的分析实现过程。EF框架实际是由XML模型建立的。如图一所示:

544481331

图1 EF文件

将EF框架EDM文件(TestModel.edmx文件)使用xml编辑器打开,可以看到其XML结构,其结构主要如下所示:

image

EDM是实体数据关系映射的XML文件。EDM主要有三部分构成 CSDL,SSDL,MSL。

  1. CSDL表面的是实体数据模型结构,
  2. SSDL表示对应的数据存储的架构,
  3. CSDL实体与SSDL数据结构的关系通过MSL映射实现。

EDM是通过ADO.NET 实体数据模型生成的。简单的了解了EF框架的本质之后,首先选择了XML的处理方式(开始不了解元数据,之后会有描述),因为这是EF框架的根源,从根源入手,肯定会解决问题。在实现第一次方案之前,有必要对数据库设计进行简要的说明,在数据库设计的时候需要对字段,表设计时相关属性要尽量完善,包括说明、字段长度、是否为空等,在这有一个问题就是我们直接在数据库中写字段、表说明是无法生成EF框架内相应说明的,所有我们需要使用EFTSQLDocumentation.Generator.exe工具,具体使用方法,此处不再赘述。需要在EDM中显示字段,表的中文说明的,使用此工具即可,我们主要是为了在MVC的view生成时,可以直接生成中文标签,才需要此功能。

通过以上描述,我们基本上知道了,我们就是要通过关系映射XML文件完成我们的最初目标,最初我们使用了Xpath,XQuery技术对XML文件进行处理,基本达到了最初的要求,但是这种方式,处理起来太繁琐,后来又尝试了Linq to Entity的方式对xml进行处理,这种方式其实也是换汤不换药。使用直接操作xml的读取方式的确很直接,很容易。但是随着测试完成,数据库不断扩容,眼前的XML文件日渐庞大,想对每个节点进行操作,简直苦不堪言!思来想去,如果有一套直接操作EDM元数据的API就好了,元数据就是用来定义数据的数据,之前尽量避免操作元数据,但是对于EF框架确实是一个很好的选择。废话一大堆,也不知道表述清楚没!有了此想法,就开始各种博客园,之后终于发现了正道,qouoww写了一篇探索EDM(Entity Framework)的EDM元数据对我启发很深,果断苦海无涯,回头是岸。

下面附上TestModel.edmx文件,及使用读取XML的xmlhelper操作类:如果要验证程序,可以自己建立一个.edmx文件,然后使用xmlhelper类进行处理查看效果。下一次将讲述使用操作元数据的方式处理EF。

TestModel.edmx文件:

   1:  <?xml version="1.0" encoding="utf-8"?>
   2:  <edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx">
   3:    <!-- EF Runtime content -->
   4:    <edmx:Runtime>
   5:      <!-- SSDL content -->
   6:      <edmx:StorageModels>
   7:      <Schema Namespace="testModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
   8:          <EntityContainer Name="testModelStoreContainer">
   9:            <EntitySet Name="SB_QXD" EntityType="testModel.Store.SB_QXD" store:Type="Tables" Schema="dbo" />
  10:            <EntitySet Name="SB_QXD_BPBJ" EntityType="testModel.Store.SB_QXD_BPBJ" store:Type="Tables" Schema="dbo" />
  11:            <EntitySet Name="SB_QXD_SBXX" EntityType="testModel.Store.SB_QXD_SBXX" store:Type="Tables" Schema="dbo" />
  12:            <EntitySet Name="SB_QXD_WXXM" EntityType="testModel.Store.SB_QXD_WXXM" store:Type="Tables" Schema="dbo" />
  13:            <EntitySet Name="SB_SBGH" EntityType="testModel.Store.SB_SBGH" store:Type="Tables" Schema="dbo" />
  14:            <EntitySet Name="SB_SBGH_SBXX" EntityType="testModel.Store.SB_SBGH_SBXX" store:Type="Tables" Schema="dbo" />
  15:            <EntitySet Name="SysDepartment" EntityType="testModel.Store.SysDepartment" store:Type="Tables" Schema="dbo" />
  16:            <EntitySet Name="SysPerson" EntityType="testModel.Store.SysPerson" store:Type="Tables" Schema="dbo" />
  17:            <AssociationSet Name="FK_SB_QXD_B_REFERENCE_SB_QXD" Association="testModel.Store.FK_SB_QXD_B_REFERENCE_SB_QXD">
  18:              <End Role="SB_QXD" EntitySet="SB_QXD" />
  19:              <End Role="SB_QXD_BPBJ" EntitySet="SB_QXD_BPBJ" />
  20:            </AssociationSet>
  21:            <AssociationSet Name="FK_SB_QXD_REFERENCE_SYSDEPAR" Association="testModel.Store.FK_SB_QXD_REFERENCE_SYSDEPAR">
  22:              <End Role="SysDepartment" EntitySet="SysDepartment" />
  23:              <End Role="SB_QXD" EntitySet="SB_QXD" />
  24:            </AssociationSet>
  25:            <AssociationSet Name="FK_SB_QXD_REFERENCE_SYSPERSO" Association="testModel.Store.FK_SB_QXD_REFERENCE_SYSPERSO">
  26:              <End Role="SysPerson" EntitySet="SysPerson" />
  27:              <End Role="SB_QXD" EntitySet="SB_QXD" />
  28:            </AssociationSet>
  29:            <AssociationSet Name="FK_SB_QXD_S_REFERENCE_SB_QXD" Association="testModel.Store.FK_SB_QXD_S_REFERENCE_SB_QXD">
  30:              <End Role="SB_QXD" EntitySet="SB_QXD" />
  31:              <End Role="SB_QXD_SBXX" EntitySet="SB_QXD_SBXX" />
  32:            </AssociationSet>
  33:            <AssociationSet Name="FK_SB_QXD_W_REFERENCE_SB_QXD" Association="testModel.Store.FK_SB_QXD_W_REFERENCE_SB_QXD">
  34:              <End Role="SB_QXD" EntitySet="SB_QXD" />
  35:              <End Role="SB_QXD_WXXM" EntitySet="SB_QXD_WXXM" />
  36:            </AssociationSet>
  37:            <AssociationSet Name="FK_SB_QXD_W_REFERENCE_SYSPERSO" Association="testModel.Store.FK_SB_QXD_W_REFERENCE_SYSPERSO">
  38:              <End Role="SysPerson" EntitySet="SysPerson" />
  39:              <End Role="SB_QXD_WXXM" EntitySet="SB_QXD_WXXM" />
  40:            </AssociationSet>
  41:            <AssociationSet Name="FK_SB_SBGH__REFERENCE_SB_SBGH" Association="testModel.Store.FK_SB_SBGH__REFERENCE_SB_SBGH">
  42:              <End Role="SB_SBGH" EntitySet="SB_SBGH" />
  43:              <End Role="SB_SBGH_SBXX" EntitySet="SB_SBGH_SBXX" />
  44:            </AssociationSet>
  45:            <AssociationSet Name="FK_SB_SBGH_REFERENCE_SYSDEPAR" Association="testModel.Store.FK_SB_SBGH_REFERENCE_SYSDEPAR">
  46:              <End Role="SysDepartment" EntitySet="SysDepartment" />
  47:              <End Role="SB_SBGH" EntitySet="SB_SBGH" />
  48:            </AssociationSet>
  49:            <AssociationSet Name="FK_SB_SBGH_REFERENCE_SYSPERSO" Association="testModel.Store.FK_SB_SBGH_REFERENCE_SYSPERSO">
  50:              <End Role="SysPerson" EntitySet="SysPerson" />
  51:              <End Role="SB_SBGH" EntitySet="SB_SBGH" />
  52:            </AssociationSet>
  53:            <AssociationSet Name="FK_SYSDEPAR_部门与部门_SYSDEPAR" Association="testModel.Store.FK_SYSDEPAR_部门与部门_SYSDEPAR">
  54:              <End Role="SysDepartment" EntitySet="SysDepartment" />
  55:              <End Role="SysDepartment1" EntitySet="SysDepartment" />
  56:            </AssociationSet>
  57:            <AssociationSet Name="FK_SYSPERSO_部门与人员_SYSDEPAR" Association="testModel.Store.FK_SYSPERSO_部门与人员_SYSDEPAR">
  58:              <End Role="SysDepartment" EntitySet="SysDepartment" />
  59:              <End Role="SysPerson" EntitySet="SysPerson" />
  60:            </AssociationSet>
  61:          </EntityContainer>
  62:          <EntityType Name="SB_QXD">
  63:            <Key>
  64:              <PropertyRef Name="ID" />
  65:            </Key>
  66:            <Property Name="ID" Type="nvarchar" Nullable="false" MaxLength="36" />
  67:            <Property Name="QXD_RWRQ" Type="datetime" />
  68:            <Property Name="QXD_ZDRQ" Type="datetime" />
  69:            <Property Name="QXD_DJBM" Type="nvarchar" MaxLength="50" />
  70:            <Property Name="QXD_WXFS" Type="nvarchar" MaxLength="50" />
  71:            <Property Name="BM_ID_QXBM" Type="nvarchar" MaxLength="36" />
  72:            <Property Name="RY_ID_QXR" Type="nvarchar" MaxLength="36" />
  73:            <Property Name="QXD_DJLY" Type="nvarchar" MaxLength="50" />
  74:            <Property Name="QXD_BZ" Type="nvarchar" MaxLength="200" />
  75:            <Property Name="LYDJ_ID" Type="nvarchar" MaxLength="36" />
  76:          </EntityType>
  77:          <EntityType Name="SB_QXD_BPBJ">
  78:            <Key>
  79:              <PropertyRef Name="ID" />
  80:            </Key>
  81:            <Property Name="ID" Type="nvarchar" Nullable="false" MaxLength="36" />
  82:            <Property Name="WLKP_ID" Type="nvarchar" MaxLength="36" />
  83:            <Property Name="WLKP_GGXH" Type="nvarchar" MaxLength="100" />
  84:            <Property Name="BPBJ_YL" Type="real" />
  85:            <Property Name="WLKP_JLDW" Type="nvarchar" MaxLength="50" />
  86:            <Property Name="BPBJ_BZ" Type="nvarchar" MaxLength="200" />
  87:            <Property Name="QXD_ID" Type="nvarchar" MaxLength="36" />
  88:          </EntityType>
  89:          <EntityType Name="SB_QXD_SBXX">
  90:            <Key>
  91:              <PropertyRef Name="ID" />
  92:            </Key>
  93:            <Property Name="ID" Type="nvarchar" Nullable="false" MaxLength="36" />
  94:            <Property Name="SBKP_BM" Type="nvarchar" MaxLength="36" />
  95:            <Property Name="SBKP_MC" Type="nvarchar" MaxLength="50" />
  96:            <Property Name="SBKP_GGXH" Type="nvarchar" MaxLength="100" />
  97:            <Property Name="SBXX_WXGS" Type="real" />
  98:            <Property Name="SBXX_TJGS" Type="real" />
  99:            <Property Name="SBXX_SQKSRQ" Type="datetime" />
 100:            <Property Name="SBXX_SQJSRQ" Type="datetime" />
 101:            <Property Name="SBXX_JHKSRQ" Type="datetime" />
 102:            <Property Name="SBXX_JHJSRQ" Type="datetime" />
 103:            <Property Name="SBXX_SJKSRQ" Type="datetime" />
 104:            <Property Name="SBXX_SJJSRQ" Type="datetime" />
 105:            <Property Name="SBXX_WXNR" Type="nvarchar" MaxLength="1000" />
 106:            <Property Name="SBXX_JTQX" Type="nvarchar" MaxLength="1000" />
 107:            <Property Name="SBXX_QXYY" Type="nvarchar" MaxLength="1000" />
 108:            <Property Name="SBXX_BZ" Type="nvarchar" MaxLength="200" />
 109:            <Property Name="QXD_DI" Type="nvarchar" MaxLength="36" />
 110:          </EntityType>
 111:          <EntityType Name="SB_QXD_WXXM">
 112:            <Key>
 113:              <PropertyRef Name="ID" />
 114:            </Key>
 115:            <Property Name="ID" Type="nvarchar" Nullable="false" MaxLength="36" />
 116:            <Property Name="WXXM_ID" Type="nvarchar" MaxLength="36" />
 117:            <Property Name="WXXM_MC" Type="nvarchar" MaxLength="50" />
 118:            <Property Name="WXXM_YQ" Type="nvarchar" MaxLength="1500" />
 119:            <Property Name="WXXM_SM" Type="nvarchar" MaxLength="1500" />
 120:            <Property Name="WXXM_MBLX" Type="nvarchar" MaxLength="100" />
 121:            <Property Name="WXXM_MBZ" Type="nvarchar" MaxLength="100" />
 122:            <Property Name="WXXM_MBSX" Type="nvarchar" MaxLength="100" />
 123:            <Property Name="WXXM_MBXX" Type="nvarchar" MaxLength="100" />
 124:            <Property Name="RY_ID" Type="nvarchar" MaxLength="36" />
 125:            <Property Name="WXXM_BZ" Type="nvarchar" MaxLength="200" />
 126:            <Property Name="QXD_ID" Type="nvarchar" MaxLength="36" />
 127:          </EntityType>
 128:          <EntityType Name="SB_SBGH">
 129:            <Key>
 130:              <PropertyRef Name="ID" />
 131:            </Key>
 132:            <Property Name="ID" Type="nvarchar" Nullable="false" MaxLength="36" />
 133:            <Property Name="SBGH_YWRQ" Type="datetime" />
 134:            <Property Name="SBGH_DJRQ" Type="datetime" />
 135:            <Property Name="SBGH_DJBH" Type="nvarchar" MaxLength="50" />
 136:            <Property Name="SBGH_ZLBZ" Type="nvarchar" MaxLength="36" />
 137:            <Property Name="WLDW_ID_ZLDW" Type="nvarchar" MaxLength="50" />
 138:            <Property Name="BM_ID_JB" Type="nvarchar" MaxLength="36" />
 139:            <Property Name="RY_ID_JB" Type="nvarchar" MaxLength="36" />
 140:            <Property Name="SBZL_YJ" Type="real" />
 141:            <Property Name="SBZL_DJLY" Type="nvarchar" MaxLength="50" />
 142:            <Property Name="SBZL_BZ" Type="nvarchar" MaxLength="200" />
 143:            <Property Name="LYDJ_ID" Type="nvarchar" MaxLength="36" />
 144:          </EntityType>
 145:          <EntityType Name="SB_SBGH_SBXX">
 146:            <Key>
 147:              <PropertyRef Name="ID" />
 148:            </Key>
 149:            <Property Name="ID" Type="nvarchar" Nullable="false" MaxLength="36" />
 150:            <Property Name="SBKP_ID" Type="nvarchar" MaxLength="36" />
 151:            <Property Name="SBKP_MC" Type="nvarchar" MaxLength="50" />
 152:            <Property Name="SBKP_GGXH" Type="nvarchar" MaxLength="100" />
 153:            <Property Name="SBXX_KSRQ" Type="datetime" />
 154:            <Property Name="SBXX_JSRQ" Type="datetime" />
 155:            <Property Name="SBXX_SL" Type="real" />
 156:            <Property Name="SBXX_DJDW" Type="nvarchar" MaxLength="50" />
 157:            <Property Name="SBXX_DJ" Type="real" />
 158:            <Property Name="SBXX_SFSH" Type="bit" />
 159:            <Property Name="SBXX_JE" Type="real" />
 160:            <Property Name="SBXX_YJ" Type="real" />
 161:            <Property Name="SBXX_FCJH" Type="real" />
 162:            <Property Name="SBXX_BZ" Type="nvarchar" MaxLength="200" />
 163:            <Property Name="SBZL_ID" Type="nvarchar" MaxLength="36" />
 164:          </EntityType>
 165:          <EntityType Name="SysDepartment">
 166:            <Key>
 167:              <PropertyRef Name="Id" />
 168:            </Key>
 169:            <Property Name="Id" Type="nvarchar" Nullable="false" MaxLength="36" />
 170:            <Property Name="Name" Type="nvarchar" Nullable="false" MaxLength="200" />
 171:            <Property Name="ParentId" Type="nvarchar" MaxLength="36" />
 172:            <Property Name="Address" Type="nvarchar" MaxLength="200" />
 173:            <Property Name="Sort" Type="int" />
 174:            <Property Name="Remark" Type="nvarchar" />
 175:            <Property Name="CreateTime" Type="datetime" />
 176:            <Property Name="CreatePerson" Type="nvarchar" MaxLength="200" />
 177:            <Property Name="UpdateTime" Type="datetime" />
 178:            <Property Name="UpdatePerson" Type="nvarchar" MaxLength="200" />
 179:          </EntityType>
 180:          <EntityType Name="SysPerson">
 181:            <Key>
 182:              <PropertyRef Name="Id" />
 183:            </Key>
 184:            <Property Name="Id" Type="nvarchar" Nullable="false" MaxLength="36" />
 185:            <Property Name="Name" Type="nvarchar" Nullable="false" MaxLength="200" />
 186:            <Property Name="MyName" Type="nvarchar" MaxLength="200" />
 187:            <Property Name="Password" Type="nvarchar" Nullable="false" MaxLength="200" />
 188:            <Property Name="SurePassword" Type="nvarchar" MaxLength="200" />
 189:            <Property Name="Sex" Type="nvarchar" MaxLength="200" />
 190:            <Property Name="SysDepartmentId" Type="nvarchar" MaxLength="36" />
 191:            <Property Name="Position" Type="nvarchar" MaxLength="200" />
 192:            <Property Name="MobilePhoneNumber" Type="nvarchar" MaxLength="200" />
 193:            <Property Name="PhoneNumber" Type="nvarchar" MaxLength="200" />
 194:            <Property Name="Province" Type="nvarchar" MaxLength="200" />
 195:            <Property Name="City" Type="nvarchar" MaxLength="200" />
 196:            <Property Name="Village" Type="nvarchar" MaxLength="200" />
 197:            <Property Name="Address" Type="nvarchar" MaxLength="200" />
 198:            <Property Name="EmailAddress" Type="nvarchar" MaxLength="200" />
 199:            <Property Name="Remark" Type="decimal" />
 200:            <Property Name="State" Type="nvarchar" MaxLength="200" />
 201:            <Property Name="CreateTime" Type="datetime" />
 202:            <Property Name="CreatePerson" Type="nvarchar" MaxLength="200" />
 203:            <Property Name="UpdateTime" Type="datetime" />
 204:            <Property Name="UpdatePerson" Type="nvarchar" MaxLength="200" />
 205:            <Property Name="Version" Type="timestamp" StoreGeneratedPattern="Computed" />
 206:          </EntityType>
 207:          <Association Name="FK_SB_QXD_B_REFERENCE_SB_QXD">
 208:            <End Role="SB_QXD" Type="testModel.Store.SB_QXD" Multiplicity="0..1" />
 209:            <End Role="SB_QXD_BPBJ" Type="testModel.Store.SB_QXD_BPBJ" Multiplicity="*" />
 210:            <ReferentialConstraint>
 211:              <Principal Role="SB_QXD">
 212:                <PropertyRef Name="ID" />
 213:              </Principal>
 214:              <Dependent Role="SB_QXD_BPBJ">
 215:                <PropertyRef Name="QXD_ID" />
 216:              </Dependent>
 217:            </ReferentialConstraint>
 218:          </Association>
 219:          <Association Name="FK_SB_QXD_REFERENCE_SYSDEPAR">
 220:            <End Role="SysDepartment" Type="testModel.Store.SysDepartment" Multiplicity="0..1" />
 221:            <End Role="SB_QXD" Type="testModel.Store.SB_QXD" Multiplicity="*" />
 222:            <ReferentialConstraint>
 223:              <Principal Role="SysDepartment">
 224:                <PropertyRef Name="Id" />
 225:              </Principal>
 226:              <Dependent Role="SB_QXD">
 227:                <PropertyRef Name="BM_ID_QXBM" />
 228:              </Dependent>
 229:            </ReferentialConstraint>
 230:          </Association>
 231:          <Association Name="FK_SB_QXD_REFERENCE_SYSPERSO">
 232:            <End Role="SysPerson" Type="testModel.Store.SysPerson" Multiplicity="0..1" />
 233:            <End Role="SB_QXD" Type="testModel.Store.SB_QXD" Multiplicity="*" />
 234:            <ReferentialConstraint>
 235:              <Principal Role="SysPerson">
 236:                <PropertyRef Name="Id" />
 237:              </Principal>
 238:              <Dependent Role="SB_QXD">
 239:                <PropertyRef Name="RY_ID_QXR" />
 240:              </Dependent>
 241:            </ReferentialConstraint>
 242:          </Association>
 243:          <Association Name="FK_SB_QXD_S_REFERENCE_SB_QXD">
 244:            <End Role="SB_QXD" Type="testModel.Store.SB_QXD" Multiplicity="0..1" />
 245:            <End Role="SB_QXD_SBXX" Type="testModel.Store.SB_QXD_SBXX" Multiplicity="*" />
 246:            <ReferentialConstraint>
 247:              <Principal Role="SB_QXD">
 248:                <PropertyRef Name="ID" />
 249:              </Principal>
 250:              <Dependent Role="SB_QXD_SBXX">
 251:                <PropertyRef Name="QXD_DI" />
 252:              </Dependent>
 253:            </ReferentialConstraint>
 254:          </Association>
 255:          <Association Name="FK_SB_QXD_W_REFERENCE_SB_QXD">
 256:            <End Role="SB_QXD" Type="testModel.Store.SB_QXD" Multiplicity="0..1" />
 257:            <End Role="SB_QXD_WXXM" Type="testModel.Store.SB_QXD_WXXM" Multiplicity="*" />
 258:            <ReferentialConstraint>
 259:              <Principal Role="SB_QXD">
 260:                <PropertyRef Name="ID" />
 261:              </Principal>
 262:              <Dependent Role="SB_QXD_WXXM">
 263:                <PropertyRef Name="QXD_ID" />
 264:              </Dependent>
 265:            </ReferentialConstraint>
 266:          </Association>
 267:          <Association Name="FK_SB_QXD_W_REFERENCE_SYSPERSO">
 268:            <End Role="SysPerson" Type="testModel.Store.SysPerson" Multiplicity="0..1" />
 269:            <End Role="SB_QXD_WXXM" Type="testModel.Store.SB_QXD_WXXM" Multiplicity="*" />
 270:            <ReferentialConstraint>
 271:              <Principal Role="SysPerson">
 272:                <PropertyRef Name="Id" />
 273:              </Principal>
 274:              <Dependent Role="SB_QXD_WXXM">
 275:                <PropertyRef Name="RY_ID" />
 276:              </Dependent>
 277:            </ReferentialConstraint>
 278:          </Association>
 279:          <Association Name="FK_SB_SBGH__REFERENCE_SB_SBGH">
 280:            <End Role="SB_SBGH" Type="testModel.Store.SB_SBGH" Multiplicity="0..1" />
 281:            <End Role="SB_SBGH_SBXX" Type="testModel.Store.SB_SBGH_SBXX" Multiplicity="*" />
 282:            <ReferentialConstraint>
 283:              <Principal Role="SB_SBGH">
 284:                <PropertyRef Name="ID" />
 285:              </Principal>
 286:              <Dependent Role="SB_SBGH_SBXX">
 287:                <PropertyRef Name="SBZL_ID" />
 288:              </Dependent>
 289:            </ReferentialConstraint>
 290:          </Association>
 291:          <Association Name="FK_SB_SBGH_REFERENCE_SYSDEPAR">
 292:            <End Role="SysDepartment" Type="testModel.Store.SysDepartment" Multiplicity="0..1" />
 293:            <End Role="SB_SBGH" Type="testModel.Store.SB_SBGH" Multiplicity="*" />
 294:            <ReferentialConstraint>
 295:              <Principal Role="SysDepartment">
 296:                <PropertyRef Name="Id" />
 297:              </Principal>
 298:              <Dependent Role="SB_SBGH">
 299:                <PropertyRef Name="BM_ID_JB" />
 300:              </Dependent>
 301:            </ReferentialConstraint>
 302:          </Association>
 303:          <Association Name="FK_SB_SBGH_REFERENCE_SYSPERSO">
 304:            <End Role="SysPerson" Type="testModel.Store.SysPerson" Multiplicity="0..1" />
 305:            <End Role="SB_SBGH" Type="testModel.Store.SB_SBGH" Multiplicity="*" />
 306:            <ReferentialConstraint>
 307:              <Principal Role="SysPerson">
 308:                <PropertyRef Name="Id" />
 309:              </Principal>
 310:              <Dependent Role="SB_SBGH">
 311:                <PropertyRef Name="RY_ID_JB" />
 312:              </Dependent>
 313:            </ReferentialConstraint>
 314:          </Association>
 315:          <Association Name="FK_SYSDEPAR_部门与部门_SYSDEPAR">
 316:            <End Role="SysDepartment" Type="testModel.Store.SysDepartment" Multiplicity="0..1" />
 317:            <End Role="SysDepartment1" Type="testModel.Store.SysDepartment" Multiplicity="*" />
 318:            <ReferentialConstraint>
 319:              <Principal Role="SysDepartment">
 320:                <PropertyRef Name="Id" />
 321:              </Principal>
 322:              <Dependent Role="SysDepartment1">
 323:                <PropertyRef Name="ParentId" />
 324:              </Dependent>
 325:            </ReferentialConstraint>
 326:          </Association>
 327:          <Association Name="FK_SYSPERSO_部门与人员_SYSDEPAR">
 328:            <End Role="SysDepartment" Type="testModel.Store.SysDepartment" Multiplicity="0..1" />
 329:            <End Role="SysPerson" Type="testModel.Store.SysPerson" Multiplicity="*" />
 330:            <ReferentialConstraint>
 331:              <Principal Role="SysDepartment">
 332:                <PropertyRef Name="Id" />
 333:              </Principal>
 334:              <Dependent Role="SysPerson">
 335:                <PropertyRef Name="SysDepartmentId" />
 336:              </Dependent>
 337:            </ReferentialConstraint>
 338:          </Association>
 339:        </Schema></edmx:StorageModels>
 340:      <!-- CSDL content -->
 341:      <edmx:ConceptualModels>
 342:        <Schema Namespace="testModel" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
 343:          <EntityContainer Name="testEntities" annotation:LazyLoadingEnabled="true">
 344:            <EntitySet Name="SB_QXD" EntityType="testModel.SB_QXD" />
 345:            <EntitySet Name="SB_QXD_BPBJ" EntityType="testModel.SB_QXD_BPBJ" />
 346:            <EntitySet Name="SB_QXD_SBXX" EntityType="testModel.SB_QXD_SBXX" />
 347:            <EntitySet Name="SB_QXD_WXXM" EntityType="testModel.SB_QXD_WXXM" />
 348:            <AssociationSet Name="FK_SB_QXD_B_REFERENCE_SB_QXD" Association="testModel.FK_SB_QXD_B_REFERENCE_SB_QXD">
 349:              <End Role="SB_QXD" EntitySet="SB_QXD" />
 350:              <End Role="SB_QXD_BPBJ" EntitySet="SB_QXD_BPBJ" />
 351:            </AssociationSet>
 352:            <AssociationSet Name="FK_SB_QXD_S_REFERENCE_SB_QXD" Association="testModel.FK_SB_QXD_S_REFERENCE_SB_QXD">
 353:              <End Role="SB_QXD" EntitySet="SB_QXD" />
 354:              <End Role="SB_QXD_SBXX" EntitySet="SB_QXD_SBXX" />
 355:            </AssociationSet>
 356:            <AssociationSet Name="FK_SB_QXD_W_REFERENCE_SB_QXD" Association="testModel.FK_SB_QXD_W_REFERENCE_SB_QXD">
 357:              <End Role="SB_QXD" EntitySet="SB_QXD" />
 358:              <End Role="SB_QXD_WXXM" EntitySet="SB_QXD_WXXM" />
 359:            </AssociationSet>
 360:            <EntitySet Name="SB_SBGH" EntityType="testModel.SB_SBGH" />
 361:            <EntitySet Name="SB_SBGH_SBXX" EntityType="testModel.SB_SBGH_SBXX" />
 362:            <AssociationSet Name="FK_SB_SBGH__REFERENCE_SB_SBGH" Association="testModel.FK_SB_SBGH__REFERENCE_SB_SBGH">
 363:              <End Role="SB_SBGH" EntitySet="SB_SBGH" />
 364:              <End Role="SB_SBGH_SBXX" EntitySet="SB_SBGH_SBXX" />
 365:            </AssociationSet>
 366:            <EntitySet Name="SysDepartment" EntityType="testModel.SysDepartment" />
 367:            <AssociationSet Name="FK_SB_QXD_REFERENCE_SYSDEPAR" Association="testModel.FK_SB_QXD_REFERENCE_SYSDEPAR">
 368:              <End Role="SysDepartment" EntitySet="SysDepartment" />
 369:              <End Role="SB_QXD" EntitySet="SB_QXD" />
 370:            </AssociationSet>
 371:            <AssociationSet Name="FK_SB_SBGH_REFERENCE_SYSDEPAR" Association="testModel.FK_SB_SBGH_REFERENCE_SYSDEPAR">
 372:              <End Role="SysDepartment" EntitySet="SysDepartment" />
 373:              <End Role="SB_SBGH" EntitySet="SB_SBGH" />
 374:            </AssociationSet>
 375:            <AssociationSet Name="FK_SYSDEPAR_部门与部门_SYSDEPAR" Association="testModel.FK_SYSDEPAR_部门与部门_SYSDEPAR">
 376:              <End Role="SysDepartment" EntitySet="SysDepartment" />
 377:              <End Role="SysDepartment1" EntitySet="SysDepartment" />
 378:            </AssociationSet>
 379:            <EntitySet Name="SysPerson" EntityType="testModel.SysPerson" />
 380:            <AssociationSet Name="FK_SB_QXD_REFERENCE_SYSPERSO" Association="testModel.FK_SB_QXD_REFERENCE_SYSPERSO">
 381:              <End Role="SysPerson" EntitySet="SysPerson" />
 382:              <End Role="SB_QXD" EntitySet="SB_QXD" />
 383:            </AssociationSet>
 384:            <AssociationSet Name="FK_SB_QXD_W_REFERENCE_SYSPERSO" Association="testModel.FK_SB_QXD_W_REFERENCE_SYSPERSO">
 385:              <End Role="SysPerson" EntitySet="SysPerson" />
 386:              <End Role="SB_QXD_WXXM" EntitySet="SB_QXD_WXXM" />
 387:            </AssociationSet>
 388:            <AssociationSet Name="FK_SB_SBGH_REFERENCE_SYSPERSO" Association="testModel.FK_SB_SBGH_REFERENCE_SYSPERSO">
 389:              <End Role="SysPerson" EntitySet="SysPerson" />
 390:              <End Role="SB_SBGH" EntitySet="SB_SBGH" />
 391:            </AssociationSet>
 392:            <AssociationSet Name="FK_SYSPERSO_部门与人员_SYSDEPAR" Association="testModel.FK_SYSPERSO_部门与人员_SYSDEPAR">
 393:              <End Role="SysDepartment" EntitySet="SysDepartment" />
 394:              <End Role="SysPerson" EntitySet="SysPerson" />
 395:            </AssociationSet>
 396:          </EntityContainer>
 397:          <EntityType Name="SB_QXD">
 398:            <Key>
 399:              <PropertyRef Name="ID" />
 400:            </Key>
 401:            <Property Name="ID" Type="String" Nullable="false" MaxLength="36" Unicode="true" FixedLength="false" >
 402:              <Documentation>
 403:                <Summary>主键</Summary>
 404:              </Documentation>
 405:            </Property>
 406:            <Property Name="QXD_RWRQ" Type="DateTime" />
 407:            <Property Name="QXD_ZDRQ" Type="DateTime" />
 408:            <Property Name="QXD_DJBM" Type="String" MaxLength="50" Unicode="true" FixedLength="false" />
 409:            <Property Name="QXD_WXFS" Type="String" MaxLength="50" Unicode="true" FixedLength="false" />
 410:            <Property Name="BM_ID_QXBM" Type="String" MaxLength="36" Unicode="true" FixedLength="false" />
 411:            <Property Name="RY_ID_QXR" Type="String" MaxLength="36" Unicode="true" FixedLength="false" />
 412:            <Property Name="QXD_DJLY" Type="String" MaxLength="50" Unicode="true" FixedLength="false" />
 413:            <Property Name="QXD_BZ" Type="String" MaxLength="200" Unicode="true" FixedLength="false" />
 414:            <Property Name="LYDJ_ID" Type="String" MaxLength="36" Unicode="true" FixedLength="false" />
 415:            <NavigationProperty Name="SB_QXD_BPBJ" Relationship="testModel.FK_SB_QXD_B_REFERENCE_SB_QXD" FromRole="SB_QXD" ToRole="SB_QXD_BPBJ" />
 416:            <NavigationProperty Name="SB_QXD_SBXX" Relationship="testModel.FK_SB_QXD_S_REFERENCE_SB_QXD" FromRole="SB_QXD" ToRole="SB_QXD_SBXX" />
 417:            <NavigationProperty Name="SB_QXD_WXXM" Relationship="testModel.FK_SB_QXD_W_REFERENCE_SB_QXD" FromRole="SB_QXD" ToRole="SB_QXD_WXXM" />
 418:            <NavigationProperty Name="SysDepartment" Relationship="testModel.FK_SB_QXD_REFERENCE_SYSDEPAR" FromRole="SB_QXD" ToRole="SysDepartment" />
 419:            <NavigationProperty Name="SysPerson" Relationship="testModel.FK_SB_QXD_REFERENCE_SYSPERSO" FromRole="SB_QXD" ToRole="SysPerson" />
 420:          </EntityType>
 421:          <EntityType Name="SB_QXD_BPBJ">
 422:            <Key>
 423:              <PropertyRef Name="ID" />
 424:            </Key>
 425:            <Property Name="ID" Type="String" Nullable="false" MaxLength="36" Unicode="true" FixedLength="false" />
 426:            <Property Name="WLKP_ID" Type="String" MaxLength="36" Unicode="true" FixedLength="false" />
 427:            <Property Name="WLKP_GGXH" Type="String" MaxLength="100" Unicode="true" FixedLength="false" />
 428:            <Property Name="BPBJ_YL" Type="Single" />
 429:            <Property Name="WLKP_JLDW" Type="String" MaxLength="50" Unicode="true" FixedLength="false" />
 430:            <Property Name="BPBJ_BZ" Type="String" MaxLength="200" Unicode="true" FixedLength="false" />
 431:            <Property Name="QXD_ID" Type="String" MaxLength="36" Unicode="true" FixedLength="false" />
 432:            <NavigationProperty Name="SB_QXD" Relationship="testModel.FK_SB_QXD_B_REFERENCE_SB_QXD" FromRole="SB_QXD_BPBJ" ToRole="SB_QXD" />
 433:          </EntityType>
 434:          <EntityType Name="SB_QXD_SBXX">
 435:            <Key>
 436:              <PropertyRef Name="ID" />
 437:            </Key>
 438:            <Property Name="ID" Type="String" Nullable="false" MaxLength="36" Unicode="true" FixedLength="false" />
 439:            <Property Name="SBKP_BM" Type="String" MaxLength="36" Unicode="true" FixedLength="false" />
 440:            <Property Name="SBKP_MC" Type="String" MaxLength="50" Unicode="true" FixedLength="false" />
 441:            <Property Name="SBKP_GGXH" Type="String" MaxLength="100" Unicode="true" FixedLength="false" />
 442:            <Property Name="SBXX_WXGS" Type="Single" />
 443:            <Property Name="SBXX_TJGS" Type="Single" />
 444:            <Property Name="SBXX_SQKSRQ" Type="DateTime" />
 445:            <Property Name="SBXX_SQJSRQ" Type="DateTime" />
 446:            <Property Name="SBXX_JHKSRQ" Type="DateTime" />
 447:            <Property Name="SBXX_JHJSRQ" Type="DateTime" />
 448:            <Property Name="SBXX_SJKSRQ" Type="DateTime" />
 449:            <Property Name="SBXX_SJJSRQ" Type="DateTime" />
 450:            <Property Name="SBXX_WXNR" Type="String" MaxLength="1000" Unicode="true" FixedLength="false" />
 451:            <Property Name="SBXX_JTQX" Type="String" MaxLength="1000" Unicode="true" FixedLength="false" />
 452:            <Property Name="SBXX_QXYY" Type="String" MaxLength="1000" Unicode="true" FixedLength="false" />
 453:            <Property Name="SBXX_BZ" Type="String" MaxLength="200" Unicode="true" FixedLength="false" />
 454:            <Property Name="QXD_DI" Type="String" MaxLength="36" Unicode="true" FixedLength="false" />
 455:            <NavigationProperty Name="SB_QXD" Relationship="testModel.FK_SB_QXD_S_REFERENCE_SB_QXD" FromRole="SB_QXD_SBXX" ToRole="SB_QXD" />
 456:          </EntityType>
 457:          <EntityType Name="SB_QXD_WXXM">
 458:            <Key>
 459:              <PropertyRef Name="ID" />
 460:            </Key>
 461:            <Property Name="ID" Type="String" Nullable="false" MaxLength="36" Unicode="true" FixedLength="false" />
 462:            <Property Name="WXXM_ID" Type="String" MaxLength="36" Unicode="true" FixedLength="false" />
 463:            <Property Name="WXXM_MC" Type="String" MaxLength="50" Unicode="true" FixedLength="false" />
 464:            <Property Name="WXXM_YQ" Type="String" MaxLength="1500" Unicode="true" FixedLength="false" />
 465:            <Property Name="WXXM_SM" Type="String" MaxLength="1500" Unicode="true" FixedLength="false" />
 466:            <Property Name="WXXM_MBLX" Type="String" MaxLength="100" Unicode="true" FixedLength="false" />
 467:            <Property Name="WXXM_MBZ" Type="String" MaxLength="100" Unicode="true" FixedLength="false" />
 468:            <Property Name="WXXM_MBSX" Type="String" MaxLength="100" Unicode="true" FixedLength="false" />
 469:            <Property Name="WXXM_MBXX" Type="String" MaxLength="100" Unicode="true" FixedLength="false" />
 470:            <Property Name="RY_ID" Type="String" MaxLength="36" Unicode="true" FixedLength="false" />
 471:            <Property Name="WXXM_BZ" Type="String" MaxLength="200" Unicode="true" FixedLength="false" />
 472:            <Property Name="QXD_ID" Type="String" MaxLength="36" Unicode="true" FixedLength="false" />
 473:            <NavigationProperty Name="SB_QXD" Relationship="testModel.FK_SB_QXD_W_REFERENCE_SB_QXD" FromRole="SB_QXD_WXXM" ToRole="SB_QXD" />
 474:            <NavigationProperty Name="SysPerson" Relationship="testModel.FK_SB_QXD_W_REFERENCE_SYSPERSO" FromRole="SB_QXD_WXXM" ToRole="SysPerson" />
 475:          </EntityType>
 476:          <Association Name="FK_SB_QXD_B_REFERENCE_SB_QXD">
 477:            <End Role="SB_QXD" Type="testModel.SB_QXD" Multiplicity="0..1" />
 478:            <End Role="SB_QXD_BPBJ" Type="testModel.SB_QXD_BPBJ" Multiplicity="*" />
 479:            <ReferentialConstraint>
 480:              <Principal Role="SB_QXD">
 481:                <PropertyRef Name="ID" />
 482:              </Principal>
 483:              <Dependent Role="SB_QXD_BPBJ">
 484:                <PropertyRef Name="QXD_ID" />
 485:              </Dependent>
 486:            </ReferentialConstraint>
 487:          </Association>
 488:          <Association Name="FK_SB_QXD_S_REFERENCE_SB_QXD">
 489:            <End Role="SB_QXD" Type="testModel.SB_QXD" Multiplicity="0..1" />
 490:            <End Role="SB_QXD_SBXX" Type="testModel.SB_QXD_SBXX" Multiplicity="*" />
 491:            <ReferentialConstraint>
 492:              <Principal Role="SB_QXD">
 493:                <PropertyRef Name="ID" />
 494:              </Principal>
 495:              <Dependent Role="SB_QXD_SBXX">
 496:                <PropertyRef Name="QXD_DI" />
 497:              </Dependent>
 498:            </ReferentialConstraint>
 499:          </Association>
 500:          <Association Name="FK_SB_QXD_W_REFERENCE_SB_QXD">
 501:            <End Role="SB_QXD" Type="testModel.SB_QXD" Multiplicity="0..1" />
 502:            <End Role="SB_QXD_WXXM" Type="testModel.SB_QXD_WXXM" Multiplicity="*" />
 503:            <ReferentialConstraint>
 504:              <Principal Role="SB_QXD">
 505:                <PropertyRef Name="ID" />
 506:              </Principal>
 507:              <Dependent Role="SB_QXD_WXXM">
 508:                <PropertyRef Name="QXD_ID" />
 509:              </Dependent>
 510:            </ReferentialConstraint>
 511:          </Association>
 512:          <EntityType Name="SB_SBGH">
 513:            <Key>
 514:              <PropertyRef Name="ID" />
 515:            </Key>
 516:            <Property Type="String" Name="ID" Nullable="false" MaxLength="36" FixedLength="false" Unicode="true" />
 517:            <Property Type="DateTime" Name="SBGH_YWRQ" />
 518:            <Property Type="DateTime" Name="SBGH_DJRQ" />
 519:            <Property Type="String" Name="SBGH_DJBH" MaxLength="50" FixedLength="false" Unicode="true" />
 520:            <Property Type="String" Name="SBGH_ZLBZ" MaxLength="36" FixedLength="false" Unicode="true" />
 521:            <Property Type="String" Name="WLDW_ID_ZLDW" MaxLength="50" FixedLength="false" Unicode="true" />
 522:            <Property Type="String" Name="BM_ID_JB" MaxLength="36" FixedLength="false" Unicode="true" />
 523:            <Property Type="String" Name="RY_ID_JB" MaxLength="36" FixedLength="false" Unicode="true" />
 524:            <Property Type="Single" Name="SBZL_YJ" />
 525:            <Property Type="String" Name="SBZL_DJLY" MaxLength="50" FixedLength="false" Unicode="true" />
 526:            <Property Type="String" Name="SBZL_BZ" MaxLength="200" FixedLength="false" Unicode="true" />
 527:            <Property Type="String" Name="LYDJ_ID" MaxLength="36" FixedLength="false" Unicode="true" />
 528:            <NavigationProperty Name="SB_SBGH_SBXX" Relationship="testModel.FK_SB_SBGH__REFERENCE_SB_SBGH" FromRole="SB_SBGH" ToRole="SB_SBGH_SBXX" />
 529:            <NavigationProperty Name="SysDepartment" Relationship="testModel.FK_SB_SBGH_REFERENCE_SYSDEPAR" FromRole="SB_SBGH" ToRole="SysDepartment" />
 530:            <NavigationProperty Name="SysPerson" Relationship="testModel.FK_SB_SBGH_REFERENCE_SYSPERSO" FromRole="SB_SBGH" ToRole="SysPerson" />
 531:          </EntityType>
 532:          <EntityType Name="SB_SBGH_SBXX">
 533:            <Key>
 534:              <PropertyRef Name="ID" />
 535:            </Key>
 536:            <Property Type="String" Name="ID" Nullable="false" MaxLength="36" FixedLength="false" Unicode="true" />
 537:            <Property Type="String" Name="SBKP_ID" MaxLe
jx 标签:
   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Web;
   5:   
   6:  using System.Xml;
   7:  using System.Data.Metadata.Edm;
   8:   
   9:  namespace MvcApplication3.Models.Helper
  10:  {
  11:      public class XMLHelper
  12:      {
  13:          /// <summary> 
  14:          /// 获取主表从表外键关系 
  15:          /// </summary> 
  16:          /// <param name="FK"></param> 
  17:          /// <returns></returns> 
  18:          public SortedList<string, string> GetTableRelation(string FK, HttpServerUtilityBase Server)
  19:          {
  20:              XmlDocument doc = new XmlDocument();
  21:              string strr = Server.MapPath("~\\Models\\TestModel.edmx");
  22:              doc.Load(strr);
  23:              XmlNodeList nodes = doc.GetElementsByTagName("Association");
  24:              SortedList<string, string> Relationlist = new SortedList<string, string>();
  25:   
  26:              foreach (XmlNode nodevalue in nodes)
  27:              {
  28:                  if (nodevalue.Attributes["Name"].Value == FK && Relationlist.Count.Equals(0))
  29:                  {
  30:   
  31:                      string str = nodevalue.FirstChild.Attributes["Multiplicity"].Value;
  32:                      string str1 = nodevalue.FirstChild.NextSibling.Attributes["Multiplicity"].Value;
  33:                      string str2 = nodevalue.LastChild.FirstChild.Attributes["Role"].Value;
  34:                      string str3 = nodevalue.LastChild.FirstChild.FirstChild.Attributes["Name"].Value;
  35:                      string str4 = nodevalue.LastChild.LastChild.Attributes["Role"].Value;
  36:                      string str5 = nodevalue.LastChild.LastChild.FirstChild.Attributes["Name"].Value;
  37:                      Relationlist.Add("MainRelation", str);
  38:                      Relationlist.Add("SubRelation", str1);
  39:                      Relationlist.Add("MainTable", str2);
  40:                      Relationlist.Add("MainPropertyName", str3);
  41:                      Relationlist.Add("SubTable", str4);
  42:                      Relationlist.Add("SubPropertyName", str5);
  43:                  }
  44:              }
  45:              return Relationlist;
  46:          }
  47:          /// <summary> 
  48:          /// 获取数据表的主键 
  49:          /// </summary> 
  50:          /// <returns></returns> 
  51:          public SortedList<string, string> GetTableKEY(HttpServerUtilityBase Server)
  52:          {
  53:              XmlDocument doc = new XmlDocument();
  54:              string strr = Server.MapPath("~\\Models\\TestModel.edmx");
  55:              doc.Load(strr);
  56:              XmlNodeList nodesAll = doc.GetElementsByTagName("EntityType");
  57:              SortedList<string, string> keylist = new SortedList<string, string>();
  58:              int count = nodesAll.Count;
  59:              foreach (XmlNode nodevalue in nodesAll)
  60:              {
  61:                  count--;
  62:                  if (count >= nodesAll.Count / 2)
  63:                  {
  64:                      keylist.Add(nodevalue.Attributes["Name"].Value, nodevalue.FirstChild.FirstChild.Attributes["Name"].Value);
  65:                  }
  66:              }
  67:              return keylist;
  68:          }
  69:   
  70:   
  71:          /// <summary>
  72:          /// 通过表名返回与之相关的表及关系
  73:          /// </summary>
  74:          /// <param name="Server">服务器</param>
  75:          /// <param name="TableName">表名</param>
  76:          /// <param name="IsShowRelation">是否查询关系</param>
  77:          /// <returns>与主表相关所有表的关系</returns>
  78:          public static List<TableRelation> GetTable(HttpServerUtilityBase Server, string TableName, bool IsShowRelation)
  79:          {
  80:              string xmlpath = Server.MapPath("~\\Models\\TestModel.edmx");
  81:              XmlDocument xmlnoe = new XmlDocument();
  82:              xmlnoe.Load(xmlpath);
  83:              XmlNamespaceManager xmlname = new XmlNamespaceManager(xmlnoe.NameTable);
  84:              xmlname.AddNamespace("edmx", "http://schemas.microsoft.com/ado/2008/10/edmx");
  85:              xmlname.AddNamespace("annotation", "http://schemas.microsoft.com/ado/2009/02/edm/annotation");
  86:              xmlname.AddNamespace("public", "http://schemas.microsoft.com/ado/2008/09/edm");//添加Schema节点命名空间
  87:              XmlNodeList xmlNodesET = xmlnoe.SelectNodes("//edmx:ConceptualModels/public:Schema/public:EntityType", xmlname);
  88:              XmlNodeList xmlNodesAT = xmlnoe.SelectNodes("//edmx:ConceptualModels/public:Schema/public:Association", xmlname);
  89:   
  90:              SortedList<string, SortedList<string, string>> list1 = new SortedList<string, SortedList<string, string>>();
  91:   
  92:              if (IsShowRelation)
  93:              {
  94:                  //list1.Add(TableName, new SortedList<string, string>());
  95:                  foreach (XmlNode nodeAT in xmlNodesAT)
  96:                  {
  97:                      if (nodeAT.FirstChild.Attributes["Role"].Value.Equals(TableName))
  98:                      {
  99:                          SortedList<string, string> list = new SortedList<string, string>();
 100:                          list.Add("Name", nodeAT.Attributes["Name"].Value);//外键关系名称
 101:                          list.Add("MainRole", nodeAT.FirstChild.Attributes["Role"].Value);//主表
 102:                          list.Add("MainMultiplicity", nodeAT.FirstChild.Attributes["Multiplicity"].Value);//映射关系 n..n
 103:                          list.Add("MainKey", nodeAT.ChildNodes[2].FirstChild.FirstChild.Attributes["Name"].Value);//主键
 104:                          list.Add("SubRole", nodeAT.FirstChild.NextSibling.Attributes["Role"].Value);//子表
 105:                          list.Add("SubMultiplicity", nodeAT.FirstChild.NextSibling.Attributes["Multiplicity"].Value);//映射关系
 106:                          list.Add("SubKey", nodeAT.ChildNodes[2].LastChild.FirstChild.Attributes["Name"].Value);//外键
 107:                          list1.Add(nodeAT.FirstChild.NextSibling.Attributes["Role"].Value, list);
 108:                      }
 109:                      else if (nodeAT.FirstChild.NextSibling.Attributes["Role"].Value.Equals(TableName))
 110:                      {
 111:                          SortedList<string, string> list = new SortedList<string, string>();
 112:                          list.Add("Name", nodeAT.Attributes["Name"].Value);//外键关系名称
 113:                          list.Add("MainRole", nodeAT.FirstChild.Attributes["Role"].Value);//主表
 114:                          list.Add("MainMultiplicity", nodeAT.FirstChild.Attributes["Multiplicity"].Value);//映射关系 n..n
 115:                          list.Add("MainKey", nodeAT.ChildNodes[2].FirstChild.FirstChild.Attributes["Name"].Value);//主键
 116:                          list.Add("SubRole", nodeAT.FirstChild.NextSibling.Attributes["Role"].Value);//子表
 117:                          list.Add("SubMultiplicity", nodeAT.FirstChild.NextSibling.Attributes["Multiplicity"].Value);//映射关系
 118:                          list.Add("SubKey", nodeAT.ChildNodes[2].LastChild.FirstChild.Attributes["Name"].Value);//外键
 119:                          list1.Add(nodeAT.FirstChild.Attributes["Role"].Value, list);
 120:   
 121:                          SortedList<string, string> listsub = new SortedList<string, string>();
 122:                          listsub.Add("Name", nodeAT.Attributes["Name"].Value);//外键关系名称
 123:                          listsub.Add("MainRole", nodeAT.FirstChild.Attributes["Role"].Value);//主表
 124:                          listsub.Add("MainMultiplicity", nodeAT.FirstChild.Attributes["Multiplicity"].Value);//映射关系 n..n
 125:                          listsub.Add("MainKey", nodeAT.ChildNodes[2].FirstChild.FirstChild.Attributes["Name"].Value);//主键
 126:                          listsub.Add("SubRole", nodeAT.FirstChild.NextSibling.Attributes["Role"].Value);//子表
 127:                          listsub.Add("SubMultiplicity", nodeAT.FirstChild.NextSibling.Attributes["Multiplicity"].Value);//映射关系
 128:                          listsub.Add("SubKey", nodeAT.ChildNodes[2].LastChild.FirstChild.Attributes["Name"].Value);//外键
 129:                          list1.Add(nodeAT.FirstChild.NextSibling.Attributes["Role"].Value, listsub);
 130:                      }
 131:                  }
 132:   
 133:                  if (!list1.ContainsKey(TableName))
 134:                  {
 135:                      list1.Add(TableName, new SortedList<string, string>());
 136:                  }
 137:              }
 138:              else
 139:              {
 140:                  list1.Add(TableName, new SortedList<string, string>());
 141:              }
 142:              List<TableRelation> listTableRelation = new List<TableRelation>();
 143:   
 144:              foreach (var a in list1)//遍历所有该表的关系
 145:              {
 146:                  foreach (XmlNode nodeET in xmlNodesET)//遍历所有EntityType节点的表
 147:                  {
 148:                      TableRelation tr = new TableRelation();
 149:                      if (nodeET.Attributes["Name"].Value.Equals(a.Key))//EntityType节点名是否与关系结构中的主键名相同
 150:                      {
 151:                          XmlNode xmlNodesET2 = xmlnoe.SelectSingleNode("//edmx:ConceptualModels/public:Schema/public:EntityType[@Name='" + a.Key + "']/public:Documentation", xmlname);
 152:                          XmlNodeList xmlNodesET3 = xmlnoe.SelectNodes("//edmx:ConceptualModels/public:Schema/public:EntityType[@Name='" + a.Key + "']/public:Property", xmlname);
 153:                          List<Column> listColumn = new List<Column>();
 154:                          foreach (XmlNode nodeET3 in xmlNodesET3)
 155:                          {
 156:                              Column column = new Column();
 157:                              column.columnname = nodeET3.Attributes["Name"].Value;
 158:                              column.type = nodeET3.Attributes["Type"].Value;
 159:                              column.title = nodeET3.FirstChild != null ? nodeET3.FirstChild.FirstChild.InnerText : nodeET3.Attributes["Name"].Value;
 160:                              column.nullable = nodeET3.Attributes["Nullable"] != null ? nodeET3.Attributes["Nullable"].Value : null;
 161:                              column.maxlength = nodeET3.Attributes["MaxLength"] != null ? nodeET3.Attributes["MaxLength"].Value : null;
 162:                              if (a.Value.Count > 0 && nodeET3.Attributes["Name"].Value.Equals(a.Value["SubKey"])
 163:                                  || a.Value.Count > 0 && nodeET3.Attributes["Name"].Value.Equals(a.Value["MainKey"]))
 164:                              {
 165:                                  column.mainrole = a.Value["MainRole"];
 166:                                  column.mainkey = a.Value["MainKey"];
 167:                                  column.relationname = a.Value["Name"];
 168:                                  if (a.Value["MainMultiplicity"].Equals("*"))
 169:                                  {
 170:                                      column.mainmultiplicity = "ManyToOne";
 171:                                  }
 172:                                  else if (a.Value["MainMultiplicity"].Equals("0..1"))
 173:                                  {
 174:                                      column.mainmultiplicity = "OneToMany";
 175:                                  }
 176:   
 177:                                  column.subrole = a.Value["SubRole"];
 178:                                  column.subkey = a.Value["SubKey"];
 179:                                  if (a.Value["SubMultiplicity"].Equals("*"))
 180:                                  {
 181:                                      column.submultiplicity = "ManyToOne";
 182:                                  }
 183:                                  else if (a.Value["SubMultiplicity"].Equals("0..1"))
 184:                                  {
 185:                                      column.submultiplicity = "OneToMany";
 186:                                  }
 187:                              }
 188:                              listColumn.Add(column);
 189:                          }
 190:                          tr.tablename = a.Key;
 191:                          tr.tabletitle = xmlNodesET2 != null ? xmlNodesET2.InnerText : a.Key;
 192:                          tr.columns = listColumn;
 193:                          listTableRelation.Add(tr);
 194:                      }
 195:                  }
 196:              }
 197:              return listTableRelation;
 198:          }
 199:   
 200:       
 201:      }
 202:  }
   
  
    
jx 标签:
   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:  using Newtonsoft.Json;
   6:   
   7:  namespace EF_XML_TableRelation
   8:  {
   9:      /// <summary>
  10:      /// 数据表列信息
  11:      /// </summary>
  12:      public class Column
  13:      {
  14:          /// <summary>
  15:          /// 列名
  16:          /// </summary>
  17:          [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
  18:          public string columnname { get; set; }
  19:          /// <summary>
  20:          /// 列备注
  21:          /// </summary>
  22:          [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
  23:          public string title { get; set; }
  24:          /// <summary>
  25:          /// 列类型
  26:          /// </summary>
  27:          [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
  28:          public string type { get; set; }
  29:          /// <summary>
  30:          /// 是否为空
  31:          /// </summary>
  32:          [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
  33:          public string nullable { get; set; }
  34:          /// <summary>
  35:          /// 最大长度
  36:          /// </summary>
  37:          [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
  38:          public string maxlength { get; set; }
  39:          /// <summary>
  40:          /// 主表名称
  41:          /// </summary>
  42:          [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
  43:          public string mainrole { get; set; }
  44:          /// <summary>
  45:          /// 主表主键
  46:          /// </summary>
  47:          [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
  48:          public string mainkey { get; set; }
  49:          /// <summary>
  50:          /// 关系名称
  51:          /// </summary>
  52:          [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
  53:          public string relationname { get; set; }
  54:          /// <summary>
  55:          /// 主表关系
  56:          /// </summary>
  57:          [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
  58:          public string mainmultiplicity { get; set; }
  59:          /// <summary>
  60:          /// 子表名称
  61:          /// </summary>
  62:          [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
  63:          public string subrole { get; set; }
  64:          /// <summary>
  65:          /// 子表外键
  66:          /// </summary>
  67:          [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
  68:          public string subkey { get; set; }
  69:          /// <summary>
  70:          /// 子表关系
  71:          /// </summary>
  72:          [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
  73:          public string submultiplicity { get; set; }
  74:          public Column()
  75:          {
  76:   
  77:          }
  78:   
  79:          public Column(string _name, string _title, string _type,
  80:             string _mainRole, string _mainKey, string _relationName,
  81:             string _mainMultiplicity, string _subRole, string _subKey,
  82:              string _subMultiplicity)
  83:          {
  84:              columnname = _name;
  85:              title = _title;
  86:              type = _type;
  87:              mainrole = _mainRole;
  88:              mainkey = _mainKey;
  89:              relationname = _relationName;
  90:              mainmultiplicity = _mainMultiplicity;
  91:              subrole = _subRole;
  92:              subkey = _subKey;
  93:              submultiplicity = _subMultiplicity;
  94:          }
  95:      }
  96:  }
jx 标签:
   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:   
   6:  namespace EF_XML_TableRelation.XML
   7:  {
   8:      /// <summary>
   9:      /// 数据表关系
  10:      /// </summary>
  11:      public class TableRelation
  12:      {
  13:          public string tablename { get; set; }
  14:          public string tabletitle { get; set; }
  15:          public List<Column> columns { get; set; }
  16:          public TableRelation()
  17:          {
  18:   
  19:          }
  20:          public TableRelation(string _name, string _tabletitle, List<Column> _column)
  21:          {
  22:              tablename = _name;
  23:              tabletitle = _tabletitle;
  24:              columns = _column;
  25:          }
  26:      }
  27:  }

你可能感兴趣的:(framework)