EF Core : Database First Conversion : Foreign Key wrong column

Viewed 982

I am trying to embrace asp.net core by migrating my existing application from .net 4.6.2 with EF6 to .net Core 2.2 with EF Core.

Given that I have an existing database I generated the model from the database to move start with. All of my column names use underscores and where foreign keys live they all start with the table name.

With EF Core it insists that my code column names change from Manager_UserLogin_ID to ManagerUserLoginId which is fine and I can go through my code

The problem arises when I try and query the UserLogin and Manager using the include

__DB.UserLogin.Include(x=>x.Manager).First();

The query trying to run tries to include a UserLoginId column in the Manager query as well as the Manager_UserLogin_ID column.

The Classes are as follows

public partial class Manager
{
    public int ManagerId { get; set; }
    public int ManagerCompanyId { get; set; }
    public int ManagerUserLoginId { get; set; }
    public virtual Company ManagerCompany { get; set; }
    public virtual UserLogin ManagerUserLogin { get; set; }
}

public partial class UserLogin
{
    public UserLogin()
    {
        Manager = new HashSet<Manager>();
    }

    public int UserLoginId { get; set; }
    public string FullName { get; set; }

    public virtual ICollection<Manager> Manager { get; set; }
}

public partial class Company
{
    public Company()
    {
        Manager = new HashSet<Manager>();
    }

    public int CompanyId { get; set; }
    public string CompanyName { get; set; }

    public virtual ICollection<Manager> Manager { get; set; }
}

The EF Core context has the following

protected override void OnModelCreating(ModelBuilder modelBuilder
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<Company>(entity =>
    {
        entity.HasKey(e => e.CompanyId).HasName("PK_Company_ID");
        entity.Property(e => e.CompanyId).HasColumnName("Company_ID");

        entity.Property(e => e.CompanyName)
            .IsRequired()
            .HasColumnName("Company_Name")
            .HasMaxLength(200);

    modelBuilder.Entity<Manager>(entity =>
    {
        entity.HasKey(e => e.ManagerId)
            .HasName("PK_Manager_ID")
            .ForSqlServerIsClustered(false);

        entity.HasIndex(e => new { e.ManagerCompanyId, e.ManagerUserLoginId })
            .HasName("IX_Manager")
            .IsUnique()
            .ForSqlServerIsClustered();

        entity.Property(e => e.ManagerId).HasColumnName("Manager_ID");

        entity.Property(e => e.ManagerCompanyId).HasColumnName("Manager_Company_ID");

        entity.Property(e => e.ManagerUserLoginId).HasColumnName("Manager_UserLogin_ID");

        entity.HasOne(d => d.ManagerCompany)
            .WithMany(p => p.Manager)
            .HasForeignKey(d => d.ManagerCompanyId)
            .OnDelete(DeleteBehavior.ClientSetNull)
            .HasConstraintName("FK_Manager_Company_ID");

        entity.HasOne(d => d.ManagerUserLogin)
            .WithMany(p => p.Manager)
            .HasForeignKey(d => d.ManagerUserLoginId)
            .OnDelete(DeleteBehavior.ClientSetNull)
            .HasConstraintName("FK_Manager_UserLogin_ID");
        });

    modelBuilder.Entity<UserLogin>(entity =>
    {
        entity.HasKey(e => e.UserLoginId).HasName("PK_UserLogin_ID");
        entity.HasIndex(e => e.UserLoginUid)
            .HasName("IX_UserLogin_UID")
            .IsUnique();

        entity.Property(e => e.UserLoginId).HasColumnName("UserLogin_ID");

        entity.Property(e => e.FullName)
            .IsRequired()
            .HasColumnName("Full_Name")
            .HasMaxLength(300);

        entity.Property(e => e.UserLoginUid).HasColumnName("UserLogin_UID");
    });

And here is the query from Sql Profiler



    exec sp_executesql N'SELECT [x.Manager].[Manager_ID], [x.Manager].[Manager_Company_ID], [x.Manager].[Manager_UserLogin_ID], , [x.Manager].[UserLoginId]
    FROM [Manager] AS [x.Manager]
    INNER JOIN (
        SELECT TOP(1) [x0].[UserLogin_ID]
        FROM [UserLogin] AS [x0]
        WHERE [x0].[UserLogin_UID] = @____UID_0
        ORDER BY [x0].[UserLogin_ID]
    ) AS [t] ON [x.Manager].[Manager_UserLogin_ID] = [t].[UserLogin_ID]
    ORDER BY [t].[UserLogin_ID]',N'@____UID_0 uniqueidentifier',@____UID_0='F55C9BBB-C7FF-4C87-B834-B2FE6B0F0B17'

Please can someone help my diagnose and fix this issue?

1 Answers

I´ve tried a few things yesterday and I was fine with changing the propertyNames:

Change public int ManagerUserLoginId { get; set; } to public int Manager_UserLogin_Id { get; set; } and remove entity.Property(e => e.Manager_UserLogin_Id).HasColumnName("Manager_UserLogin_ID"); from your modelBuilder.

That is a quickFix.. The problem behind that seems to be a bigger problem. I would create a new Database with the same configuration/classes VS created for you.

  • Create a new Project
  • Add a connection string with a new DB-Name
  • Paste in your classes
  • Create a new DbContext
  • Go to package manager console and paste in add-migration InitialMigration

Ef Core will create a migration to create a new database, based on your model an configuration. Attention: Be sure that your connectionString does not point to any existing Database. If you execute the migration, you´ll lose the data in any existing database!

  • If you are absolutely sure that you point to a new DB paste in update-database

After that you can insert new Data to your TestDB to check where the problem is.

Related