I have a project that has a crowded entity model. Most of the tables are related to each other (one to many, many to many and many to one). I need to get all table names related to each other.
With the code below, I can get all props of a class and eliminate the types by Split method to get other tables related to that class.
List<string> tabloIsimleri = new List<string>();
public object GetTableList()
{
var List = dbContext.Takips.ToList();
foreach (var item in List)
{
foreach (_PropertyInfo p in typeof(Takips).GetProperties())
{
var propName = p.PropertyType.FullName;
if (propName.Contains("ProjectName.DataService"))
{
string[] test = p.Name.Split(' ');
int a = test.Count();
var son = test[a - 1];
if (!tabloIsimleri.Contains(son))
{
tabloIsimleri.Add(son);
}
}
}
}
return tabloIsimleri;
}
This method brings back all tables related with "Takips". Apart from doing this manually for all of the tables, how can I reach all related ones?
Let's say I create a new Dictionary<string, List<string>>
and set the keys as the table names, and populate the List<string>
with table names related to the table which is set as the key. By this way I may hold all of them. However, I could not plan the scenario.
I've managed it. Here, how it's done:
public class ReportService
{
EntityName dbContext = new EntityName();
List<string> tabloIsimleri = new List<string>();
List<string> tabloListesi = new List<string>();
List<string> iliskiliOlanlar = new List<string>();
Dictionary<string, List<string>> agacGosterim = new Dictionary<string, List<string>>();
public void Test()
{
try
{
tabloListesi = Tablolar();
string tutmac;
foreach (var t in tabloListesi)
{
tutmac = string.Format("ProjectName.DataService.{0}", t);
var tip = Type.GetType(tutmac, true);
foreach (_PropertyInfo p in tip.GetProperties())
{
var propName = p.PropertyType.FullName;
if (propName.Contains("ProjectName.DataService"))
{
string[] test = p.Name.Split(' ');
int a = test.Count();
var son = test[a - 1];
if (!iliskiliOlanlar.Contains(son))
{
iliskiliOlanlar.Add(son);
}
}
}
agacGosterim.Add(t, iliskiliOlanlar);
iliskiliOlanlar = new List<string>();
}
}
catch (Exception)
{
}
}
public List<string> Tablolar()
{
using (var context = new EntityName())
{
var objectContext = ((IObjectContextAdapter)context).ObjectContext;
var storageMetadata = ((EntityConnection)objectContext.Connection).GetMetadataWorkspace().GetItems(DataSpace.SSpace);
var entityProps = (from s in storageMetadata where s.BuiltInTypeKind == BuiltInTypeKind.EntityType select s as EntityType);
foreach (var item in entityProps)
{
string[] ayir = item.FullName.Split('.');
int a = ayir.Count() - 1;
if (!tabloIsimleri.Contains(ayir[a]))
{
tabloIsimleri.Add(ayir[a]);
}
}
return tabloIsimleri;
}
}
}