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

Created Unassigned: More than 2 FK relationship problem [50]

$
0
0
Version 2.3.0
I've got a customer table and a trainer table, and I need to track 2 relationships per customer: Their Trainer, and their Sponsor (both are also customers).
If there's more than 2 FK relationships between tables, the reverse navigation property names revert to old behavior.
With 2 FK relationships:
```
public class Customer
{
public string CustomerCode { get; set; } // CustomerCode (Primary key)

// Reverse navigation
public virtual ICollection<Trainer> Trainers_TrainerCode { get; set; } // Trainer.FK_Trainer_Customer_Trainer
public virtual Trainer Trainers_CustomerCode { get; set; } // Trainer.FK_Trainer_Customer
```
With 3 FK relationships:
```
public class Customer
{
public string CustomerCode { get; set; } // CustomerCode (Primary key)

// Reverse navigation
public virtual ICollection<Trainer> Trainers1 { get; set; } // Trainer.FK_Trainer_Customer_Sponsor
public virtual ICollection<Trainer> Trainers2 { get; set; } // Trainer.FK_Trainer_Customer_Trainer
public virtual Trainer Trainers_CustomerCode { get; set; } // Trainer.FK_Trainer_Customer
```

Here's a script to generate the test tables:
```
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Customer](
[CustomerCode] [nvarchar](50) NOT NULL,
[CustomerName] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
(
[CustomerCode] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

CREATE TABLE [dbo].[Trainer](
[CustomerCode] [nvarchar](50) NOT NULL,
[TrainerCode] [nvarchar](50) NOT NULL,
[SponsorCode] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Trainer] PRIMARY KEY CLUSTERED
(
[CustomerCode] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[Trainer] WITH CHECK ADD CONSTRAINT [FK_Trainer_Customer] FOREIGN KEY([CustomerCode])
REFERENCES [dbo].[Customer] ([CustomerCode])
GO

ALTER TABLE [dbo].[Trainer] CHECK CONSTRAINT [FK_Trainer_Customer]
GO

ALTER TABLE [dbo].[Trainer] WITH CHECK ADD CONSTRAINT [FK_Trainer_Customer_Sponsor] FOREIGN KEY([TrainerCode])
REFERENCES [dbo].[Customer] ([CustomerCode])
GO

ALTER TABLE [dbo].[Trainer] CHECK CONSTRAINT [FK_Trainer_Customer_Sponsor]
GO

ALTER TABLE [dbo].[Trainer] WITH CHECK ADD CONSTRAINT [FK_Trainer_Customer_Trainer] FOREIGN KEY([TrainerCode])
REFERENCES [dbo].[Customer] ([CustomerCode])
GO

ALTER TABLE [dbo].[Trainer] CHECK CONSTRAINT [FK_Trainer_Customer_Trainer]
GO


```

Viewing all articles
Browse latest Browse all 1642

Latest Images

Trending Articles



Latest Images

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