如何在我的UML中检测表之间的关系

本文关键字:之间 关系 检测 我的 UML | 更新日期: 2023-09-27 18:21:28

我正在使用Visual Studio 2012在C#中开发一个应用程序,该应用程序必须生成一个通用数据模型,一个*.edmx文件。这个程序接收一个UML模型,并将其转换为MVC架构中的C#类和数据模型,所以,我还没有得到表之间的关系、PK和FK。

我的问题是生成表之间的关系(导航属性?)。对于每个数据资源(表),我必须检查它是否有外键,如果有,则生成正确的代码来正确创建edmx文件。到目前为止,我得到的是:

我的edmxCodeGen类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataComponent;
namespace codeGenAppMM2XulRdf
{
    public class edmxCodeGen
    {
        public static string toCS(AppDataModel appRDF)
        {
            StringBuilder csResult = new StringBuilder();
            csResult.AppendLine("<?xml version='"1.0'" encoding='"utf-8'"?>");
            csResult.AppendLine("<edmx:Edmx Version='"3.0'" xmlns:edmx='"http://schemas.microsoft.com/ado/2009/11/edmx'">");
            csResult.AppendLine("<!-- EF Runtime content -->");
            csResult.AppendLine("<edmx:Runtime>");
            csResult.AppendLine("<!-- SSDL content -->");
            csResult.AppendLine("<edmx:StorageModels>");
            csResult.AppendLine("  <Schema Namespace='"BDClinicModel.Store'" Alias='"Self'" Provider='"System.Data.SqlClient'" ProviderManifestToken='"2005'" xmlns:store='"http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator'" xmlns='"http://schemas.microsoft.com/ado/2009/11/edm/ssdl'">");
            csResult.AppendLine("    <EntityContainer Name='"BDClinicModelStoreContainer'">");
            // ver namespace e container name nas duas linhas acima
            foreach (DataResource dr in appRDF.dataResources)
            {
                csResult.AppendLine("      <EntitySet Name=" + dr.ResourceClassName + " EntityType='"BDClinicModel.Store." + dr.ResourceClassName + " store:Type='"Tables'" Schema='"dbo'" />");
            }
            csResult.AppendLine("      <EntitySet Name='"sysdiagrams'" EntityType='"BDClinicModel.Store.sysdiagrams'" store:Type='"Tables'" Schema='"dbo'" />");
            // sysdiagrams
            foreach (DataResource dr in appRDF.dataResources)
            {
                foreach (NavigationProperty np in dr.NavigationAttrs)
                {

                }
            }
            csResult.AppendLine("");
            csResult.AppendLine("");
            csResult.AppendLine("");
            csResult.AppendLine("");
            return csResult.ToString();
        }
    }
}

我打算使用最后两个"foreach"周期来写我缺少的东西,比如这样(来自其他例子,在这个例子中,我导入了一个数据库,edmx是自动生成的):

  <AssociationSet Name="FK__consulta__id_mar__3493CFA7" Association="BDClinicModel.Store.FK__consulta__id_mar__3493CFA7">
    <End Role="marcacao" EntitySet="marcacao" />
    <End Role="consulta" EntitySet="consulta" />
  </AssociationSet>
  <AssociationSet Name="FK__consulta__id_pro__3587F3E0" Association="BDClinicModel.Store.FK__consulta__id_pro__3587F3E0">
    <End Role="processo" EntitySet="processo" />
    <End Role="consulta" EntitySet="consulta" />
  </AssociationSet>
...

问题是,我如何检测关系?我想我必须遍历数据资源并检查(也许?)导航属性,但我看不出该怎么做。

有人能帮忙吗?有什么想法吗?

谢谢,Chiapa

如何在我的UML中检测表之间的关系

我设法通过以下方式检测关系:

        foreach (UMLRelationship rel in umlDomainModel.UMLClassRelationships)
        {
            if (rel.relType == Enum_TypeOfRelation.aggregation || rel.relType == Enum_TypeOfRelation.association || rel.relType == Enum_TypeOfRelation.composition)
            { 
                csResult.AppendLine("");
...

通过这种方式,我遍历UML域模型的所有关系,然后检查关系类型是否是三种关系之一(聚合、关联或组合)。如果是,则关系存在,并且我可以使用关联的角色和类。

感谢