Quantcast
Channel: EntityFramework Reverse POCO Code First Generator
Viewing all articles
Browse latest Browse all 1642

Edited Feature: Generate navigation property based on FK name [42]

$
0
0
When there are more than one reference to a table, the generator adds a number after the destination table name in the POCO class to name navigation property. For example if we have a Product table which one User orders and another User confirms we'll have:

```
class Product
{
public virtual User User {get; set;}
public virtual User User1 {get; set;}
}
```

To make it meaningful I've made some changes (not fully tested yet) to Core tt file to generate like this:
```
class Product
{
public virtual User User_OrderedBy {get; set;}
public virtual User User_ConfirmedBy {get; set;}
}
```

Here are the modifications:
```
public string GetUniqueColumnPropertyName(string columnName,IList<ForeignKey> fKeys)
{
if (ReverseNavigationUniquePropName.Count == 0)
{
ReverseNavigationUniquePropName.Add(NameHumanCase);
ReverseNavigationUniquePropName.AddRange(Columns.Select(c => c.PropertyNameHumanCase));
}

if (!ReverseNavigationUniquePropName.Contains(columnName))
{
ReverseNavigationUniquePropName.Add(columnName);
return columnName;
}

for (int n = 1; n < 100; ++n)
{
string col;
if (fKeys != null && fKeys.Count>n-1)
col = columnName + "_" + fKeys[n-1].FkColumn;
else
col = columnName + n;
if (ReverseNavigationUniquePropName.Contains(col))
continue;
ReverseNavigationUniquePropName.Add(col);
return col;
}

// Give up
return columnName;
}
```
And the calls to the method (should double check):

```
string pkPropName = fkTable.GetUniqueColumnPropertyName(pkTableHumanCase, foreignKeys);
string fkPropName = pkTable.GetUniqueColumnPropertyName(fkTable.NameHumanCase, foreignKeys);
rightTable.AddReverseNavigation(Relationship.ManyToMany, rightTable.NameHumanCase, leftTable,rightTable.GetUniqueColumnPropertyName(leftTable.NameHumanCase, foreignKeys), null, collectionType);
leftTable.AddReverseNavigation(Relationship.ManyToMany, leftTable.NameHumanCase, rightTable,leftTable.GetUniqueColumnPropertyName(rightTable.NameHumanCase, foreignKeys), null, collectionType);
```


Viewing all articles
Browse latest Browse all 1642

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>