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

Commented Unassigned: Foreign Key generation doesn't work when two table names differ only in pluralization [55]

$
0
0
I have a large (300+ tables) database which has grown over many years so that some of the names are suboptimal. In particular, there are two tables called
tblReturn
-and-
tblReturns

tblReturn (no s) has a one-to-many relationship to tblReturnDetail

These two tables differ only in the final 's', which seems to be confusing the pluralization mechanism. When I generate the POCO classes, I end up with:

TblReturn.cs
TblReturn1.cs
TblReturn1Configuration.cs
TblReturnConfiguration.cs
TblReturnDetail.cs
TblReturnDetailConfiguration.cs

The error occurs in TblReturnDetailConfiguration.cs, as follows:

```
// tblReturnDetail
internal class TblReturnDetailConfiguration : EntityTypeConfiguration<TblReturnDetail>
{
public TblReturnDetailConfiguration(string schema = "dbo")
{
ToTable(schema + ".tblReturnDetail");
HasKey(x => x.ReturnDetailId);

Property(x => x.ReturnDetailId).HasColumnName("ReturnDetailID").IsRequired().HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(x => x.ReturnId).HasColumnName("ReturnID").IsRequired();
Property(x => x.StockCode).HasColumnName("StockCode").IsRequired().HasMaxLength(7);
Property(x => x.BatchNo).HasColumnName("BatchNo").IsOptional();
Property(x => x.Qty).HasColumnName("Qty").IsRequired();

// Foreign keys
HasRequired(a => a.TblReturn).WithMany(b => b.TblReturnDetails).HasForeignKey(c => c.ReturnId); // tblReturnDetail_FK00
}
}
```
The error is in the final line, b.TblReturnDetails doesn't exist, because that is actually in TblReturn1, not TblReturn. The relationship appears to have been added to the _wrong table_.

Looking at the generated classes for tblReturn and tblReturns, we see the following...
```
// tblReturns
public class TblReturn
```
```
// tblReturn
public class TblReturn1
```

I think it is pretty clear from that how things are getting confused.
Comments: I need to add the ability to turn off the pluralisation service

Viewing all articles
Browse latest Browse all 1642

Trending Articles



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