EF relationship coming from Foxpro migration, with compound key

2

I am migrating a legacy VisualFox system in .Net where the tables were made all out of relationship, in ADO, without foreign key in relationships. Only by comparing a string of the name "Type" to differentiate whether the record in a Table, eg Address belongs to Correspondent, Store or Customer.

The system is very large and there are several relationships similar to this for example: Store and Correspondent have a Contact table also listing the ID and differentiating it by the Type.

It is very difficult to mirror this rule in EF (this is possible, because I am a beginner and I am still learning). The table looks like this:

EnderecoId - ReferenceId          - Tipo                 - Logradouro
1          - 1                    - Loja                 - Rua tal tal tal
2          - 1                    - Correspondente       - Rua tal tal tal
3          - 1                    - Correspondente       - Rua tal tal tal
4          - 1                    - Cliente              - Rua tal tal tal

Can you make these rules using a Type string as a way to differentiate addresses? If it is not possible, would it be right to use Composite Key in this case?

    
asked by anonymous 09.05.2017 / 19:20

1 answer

1

The Entity Framework natively does not have this ability to intuit the type of the related table automatically, by use of a column, for example.

Instead, I would do a table in SQL to improve this table. Remembering approach 1 would add 3 new columns to your table:

ALTER TABLE Enderecos
ADD LojaId int null,
    ClienteId int null,
    CorrespondenteId int null;

It would make 3% of% s to popular these columns:

UPDATE Enderecos
SET LojaId = ReferenceId
WHERE Tipo = 'Loja' ;

UPDATE Enderecos
SET ClienteId = ReferenceId
WHERE Tipo = 'Cliente' ;

UPDATE Enderecos
SET CorrespondenteId = ReferenceId
WHERE Tipo = 'Correspondente' ;

We will therefore have:

EnderecoId + ReferenceId  + Tipo            + Logradouro      + LojaId + ClienteId + CorrespondenteId
1          | 1            | Loja            | Rua tal tal tal | 1      | null      | null
2          | 1            | Correspondente  | Rua tal tal tal | null   | 1         | null
3          | 1            | Correspondente  | Rua tal tal tal | null   | 1         | null
4          | 1            | Cliente         | Rua tal tal tal | null   | null      | 1

With this alone the Entity Framework is able to understand the relation by approach 1 (using inheritance).

    
09.05.2017 / 19:51