I’ve got a large problem with an existing database and the primary key / foreign keys using Entity Framework.
It's an existing database and it is not allowed to change the database, at least not the existing keys because they are used by other programs. To explain what is going on with the keys I added a partial diagram.
The parameterCode and Type are string index keys, so the are nog as normal Id's
The database consists of 4 tables:
Parameter
This has the following primary key columns (which form a composite key):
- ParameterCode
- Type => this key is part of the parameterType
ParameterType
This has the following primary key:
- ParameterTypeCode => connected to type in Parameter.
ParameterDocumentType
PrimaryKey: ParameterDocumentId
This has the following foreign keys
ParamaterCode=> connected to Parameter- Type => connected to parameter
ParameterDocument=> this one works fine, so I keep it out
I created configurations and models to connect all the tables, however the Parameter table connecting to the parameterType does not accept the configuration. When I declare both keys as primary keys.
When connecting the parametertype foreign key which is also part of the primary key I get the following error:
System.InvalidOperationException: 'The property or navigation 'ParameterType' cannot be added to the entity type 'ParameterModel' because a property or navigation with the same name already exists on entity type 'ParameterModel'.'
I stripped the code from all other values to make it smaller.
[
