I am trying to update the model in my ASP.NET Core application by running the Scaffold-DbContext command after I have done some changes in the Oracle database that I am creating the model for.
In that database I have a table which has an autoincrementing ID as primary key and also some more attributes. Let's name it "T_EXAMPLES" for illustration purposes. Then I have two other tables, one called "T_EXAMPLES_CONFIGURATIONS" which contains n configurations for the first table and one called "T_EXAMPLES_DATA" which contains the data for the first table. For each entry in T_EXAMPLES, there can be multiple data entries and also multiple configurations in the other two tables. When running Scaffold-DbContext -Connection Name=ConnectionName -Provider "Oracle.EntityFrameworkCore" -OutputDir "Model" -Context "TestContext" -ContextDir "DataAccess" -Force -Verbose I get the exact same output for both tables regarding their foreign keys:
Data table:
Found foreign key on table: FK_EXAMPLES_DATA#ID, name: EXAMPLE.T_EXAMPLES_DATA, principal table: EXAMPLE.T_EXAMPLES, delete action: NO ACTION.
2021-02-10 11:10:47.310632 ThreadID:1 (MAP) OracleDatabaseModelFactory.GetForeignKeysCombined() : tableName: T_EXAMPLES_DATA, tableSchema: EXAMPLE, principalTableName: T_EXAMPLES, principalTableSchema: EXAMPLE, columnName: ID, principalColumnName: ID
Configuration table:
Found foreign key on table: FK_EXAMPLES_CONFIGURATION#ID, name: EXAMPLE.T_EXAMPLES_CONFIGURATIONS, principal table: EXAMPLE.T_EXAMPLES, delete action: NO ACTION.
2021-02-10 11:10:47.311297 ThreadID:1 (MAP) OracleDatabaseModelFactory.GetForeignKeysCombined() : tableName: T_EXAMPLES_CONFIGURATIONS, tableSchema: EXAMPLE, principalTableName: T_EXAMPLES, principalTableSchema: EXAMPLE, columnName: ID, principalColumnName: ID
However, when I look into the model for T_EXAMPLES after running the command, there is only one collection property for the configurations, but none for the data:
public partial class TExamples
{
public TExamples()
{
TExamplesConfigurations = new HashSet<TExamplesConfigurations>();
}
public decimal Id { get; set; }
// ...
public virtual ICollection<TExamplesConfigurations> TExamplesConfigurations { get; set; }
}
Why is there no collection created for the data and how can I troubleshoot this problem?
I was able to solve this problem by adding a primary key to the table T_EXAMPLES_DATA. However, I still do not know why this fixed the problem. If anyone knows the reason or can link me to a source explaining this further, please let me know.