Kirsten
Kirsten

Reputation: 18160

Is there a way to force _ID convention for the column name of a foreign key?

I want to use a new .net core 5 winforms, entity framework xaf application on an old database created some time ago with .net framework.

In my database the foreign keys of the security tables have names like Role_ID where as the new application expects names like RoleId

Is there a way I can cause the _ID convention without altering the Dev Express code that controls the security table structures?

For example I do not have a DBSet for PermissionPolicyMemberPermissionsObject because I never needed it with the framework app.

In the OnModelCreating I have

    modelBuilder.Entity<PermissionPolicyMemberPermissionsObject>()
        .ToTable("PermissionPolicyMemberPermissionsObjects")
        .HasOne<PermissionPolicyTypePermissionObject>().WithMany().HasForeignKey("TypePermissionObject_ID");

Yet, if I allow a new database to create, the table creates with a TypePermissionObject_ID field and a TypePermissionObjectID field.

If I try to access the old database that just has the TypePermissionObject_ID field I get errors indicating the TypePermissionObjectID is missing.

I am targetting net5.0 with

   <PackageReference Include="DevExpress.ExpressApp" Version="21.2.5" />
    <PackageReference Include="DevExpress.ExpressApp.CodeAnalysis" Version="21.2.5" />
    <PackageReference Include="DevExpress.ExpressApp.ConditionalAppearance" Version="21.2.5" />
    <PackageReference Include="DevExpress.ExpressApp.EFCore" Version="21.2.5" />
    <PackageReference Include="DevExpress.ExpressApp.Validation" Version="21.2.5" />
    <PackageReference Include="DevExpress.Persistent.Base" Version="21.2.5" />
    <PackageReference Include="DevExpress.Persistent.BaseImpl.EFCore" Version="21.2.5" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="5.0.13" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.13" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.13" />

[Update] After studying this question I experimented with statements like

    modelBuilder.Entity<PermissionPolicyActionPermissionObject>()
        .ToTable("PermissionPolicyActionPermissionObjects")
        .HasOne<PermissionPolicyRoleBase>().WithMany().HasForeignKey("RoleID");

But I don't know how to set the Column Name. I tried adding

modelBuilder.Entity() .Property("RoleID").HasColumnName("Role_ID");

But that created an addition field called RoleID1 table structure

Upvotes: 0

Views: 528

Answers (1)

Kirsten
Kirsten

Reputation: 18160

The following pattern works.

    modelBuilder.Entity<PermissionPolicyActionPermissionObject>()
        .ToTable("PermissionPolicyActionPermissionObjects")
        .HasOne<PermissionPolicyRoleBase>(x=>x.Role).WithMany(y=>y.ActionPermissions).HasForeignKey("RoleID");

    modelBuilder.Entity<PermissionPolicyActionPermissionObject>()
       .Property<int?>("RoleID").HasColumnName("Role_ID");

Upvotes: 0

Related Questions