Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unusual navigation names in generated models from existing database #14278

Closed
singhniranjan opened this issue Dec 31, 2018 · 5 comments
Closed
Labels
area-scaffolding closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported punted-for-3.0 punted-for-6.0 punted-for-7.0 Originally planned for the EF Core 7.0 (EF7) release, but moved out due to resource constraints. type-bug
Milestone

Comments

@singhniranjan
Copy link

singhniranjan commented Dec 31, 2018

When model classes are generated for existing database, column names are in correct in the case as described below.

I have a sample database, say, school database which has two tables Student_Details and Book_Details
Book is issued to multiple students at different times, so one book to many students relationship is there. Now in Book model class, navigation property name for Student is generated incorrectly. Ideally, it should be Student_Details whereas it is generated as Student_

Script of sample database:

CREATE TABLE [dbo].[Book_Details](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Book_Name] [nvarchar](50) NULL,
	[Student_Id] [int] NULL,
 CONSTRAINT [PK_Book_Details] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[Student_Details](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Student_Name] [nvarchar](256) NULL,
 CONSTRAINT [PK_Student_Details] PRIMARY KEY CLUSTERED 
(
	[ID] 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].[Book_Details]  WITH CHECK ADD  CONSTRAINT [FK_Book_Details_Student_Details] FOREIGN KEY([Student_Id])
REFERENCES [dbo].[Student_Details] ([ID])
GO
ALTER TABLE [dbo].[Book_Details] CHECK CONSTRAINT [FK_Book_Details_Student_Details]
GO

Steps to reproduce

Run command in PM Console:
Scaffold-DbContext "Server=.\SQLEXPRESS;Database=school;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -UseDatabaseNames -Context schoolEntities

The same behavior is there if run from command line instead of PM Console.

Here are genereated model classes. Please note, property name in Book_Details class should be Student_Details, whereas it is Student_

using System;
using System.Collections.Generic;

namespace CoreData.Models
{
    public partial class Book_Details
    {
        public int ID { get; set; }
        public string Book_Name { get; set; }
        public int? Student_Id { get; set; }

        public Student_Details Student_ { get; set; }
    }
}

//--------------

using System;
using System.Collections.Generic;

namespace CoreData.Models
{
    public partial class Student_Details
    {
        public Student_Details()
        {
            Book_Details = new HashSet<Book_Details>();
        }

        public int ID { get; set; }
        public string Student_Name { get; set; }

        public ICollection<Book_Details> Book_Details { get; set; }
    }
}

Further technical details

EF Core version: 2.1.1-rtm-30846
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017

@smitpatel
Copy link
Contributor

Navigation name is derived from navigationName + PKPropertyName = FKPropertyName
In your case FK is Student_Id and PK on the other side is ID so it made judgement that navigation should be named as stripped out PK property name from FK. Student_ is not a good answer though.

@singhniranjan
Copy link
Author

In case of EF6, it generates appropriately i.e. it will generate Student_Details and not Student_
There is an existing Web Api project which uses EF6, that needs to be migrated to Web Api Core using EF Core. And so the need to reverse engineer from existing database.
If we remove underscore character, still behavior is all the same. This is only in case for primary key table. Property names for foreign key tables are generated correctly.

@ajcvickers ajcvickers added this to the 3.0.0 milestone Jan 4, 2019
@sc-vudo
Copy link

sc-vudo commented Jan 11, 2019

I have same issue where I have used the foreign key column names not matching the primary key column name.

e.g.

User table has User_ID as PK
Company table has Company_ID as PK
Manager table has FKs to
Manager_User_ID which is FK to User
Manager_Company_ID is FK to Company

When querying the DB all is well on one entity but add include to query and we hit a problem. It keeps tryingto add a column name matching the PK column name from the FK table.

@ajcvickers
Copy link
Contributor

@sc-vudo This looks different from the issue reported here. Can you please file a new issue, including a small, runnable project/solution or complete code listing that demonstrates the behavior you are seeing.

@bricelam bricelam changed the title Incorrect column names in generated models from existing database Unusual navigation names in generated models from existing database Jun 20, 2019
@divega divega modified the milestones: 3.0.0, Backlog Jun 21, 2019
@bricelam bricelam modified the milestones: Backlog, 5.0.0 Nov 5, 2019
@ajcvickers ajcvickers modified the milestones: 5.0.0, Backlog Nov 20, 2019
@ajcvickers ajcvickers modified the milestones: Backlog, 6.0.0 Nov 5, 2020
@ajcvickers ajcvickers modified the milestones: 6.0.0, Backlog Jul 30, 2021
@ajcvickers ajcvickers modified the milestones: Backlog, 7.0.0 Nov 10, 2021
@ajcvickers ajcvickers added punted-for-7.0 Originally planned for the EF Core 7.0 (EF7) release, but moved out due to resource constraints. and removed propose-punt labels Jul 7, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0, Backlog Jul 7, 2022
@ajcvickers
Copy link
Contributor

No longer repros with 7.0. Generated code is:

public partial class Student_Detail
{
    public int ID { get; set; }

    public string? Student_Name { get; set; }

    public virtual ICollection<Book_Detail> Book_Details { get; } = new List<Book_Detail>();
}
public partial class Book_Detail
{
    public int ID { get; set; }

    public string? Book_Name { get; set; }

    public int? Student_Id { get; set; }

    public virtual Student_Detail? Student { get; set; }
}

@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Dec 5, 2022
@ajcvickers ajcvickers modified the milestones: Backlog, 7.0.0 Dec 5, 2022
@ajcvickers ajcvickers assigned ajcvickers and unassigned bricelam Dec 5, 2022
ajcvickers added a commit that referenced this issue Dec 5, 2022
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-scaffolding closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported punted-for-3.0 punted-for-6.0 punted-for-7.0 Originally planned for the EF Core 7.0 (EF7) release, but moved out due to resource constraints. type-bug
Projects
None yet
Development

No branches or pull requests

7 participants