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

时间:2013-07-08 18:55:08

标签: c# .net visual-studio-2012 edmx ado.net-entity-data-model

我正在使用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

1 个答案:

答案 0 :(得分:0)

我设法以这种方式检测关系:

        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域模型的所有关系,以便事后检查关系类型是否为三者之一(聚合,关联或组合)。如果是,那么关系存在,我可以使用相关的角色和类。

由于