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

Unnecessary alternate key generated by convention #13628

Closed
FranklinWhale opened this issue Oct 15, 2018 · 1 comment · Fixed by #17481
Closed

Unnecessary alternate key generated by convention #13628

FranklinWhale opened this issue Oct 15, 2018 · 1 comment · Fixed by #17481
Labels
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 type-bug
Milestone

Comments

@FranklinWhale
Copy link

FranklinWhale commented Oct 15, 2018

An unnecessary alternate key is created for the UserListMember table in Step 2 of the Steps to Reproduce:

    CONSTRAINT "PK_UserListMembers" PRIMARY KEY ("UserListId", "MemberId"),
    CONSTRAINT "AK_UserListMembers_MemberId_UserListId" UNIQUE ("MemberId", "UserListId"),
    CONSTRAINT "FK_UserListMembers_Users_MemberId" FOREIGN KEY ("MemberId") REFERENCES "Users" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_UserListMembers_UserLists_UserListId" FOREIGN KEY ("UserListId") REFERENCES "UserLists" ("Id") ON DELETE CASCADE

That alternate key is not generated when the word User is removed from the table name (Step 5):

    CONSTRAINT "PK_ListMembers" PRIMARY KEY ("ListId", "MemberId"),
    CONSTRAINT "FK_ListMembers_Lists_ListId" FOREIGN KEY ("ListId") REFERENCES "Lists" ("Id") ON DELETE CASCADE,
    CONSTRAINT "FK_ListMembers_Users_MemberId" FOREIGN KEY ("MemberId") REFERENCES "Users" ("Id") ON DELETE CASCADE

The same behaviour is also reproducible using the Npgsql provider.

Steps to Reproduce

  1. Create a new console project
  2. Create a .cs file with content below:
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;

namespace EFCoreTest
{
    public class User
    {
        [Key]
        public string Id { get; set; }

        public ICollection<UserList> UserLists { get; set; }

        public ICollection<UserListMember> UserListMembers { get; set; }
    }

    public class UserList
    {
        [Key]
        public string Id { get; set; }

        [Required]
        public string OwnerId { get; set; }

        public User Owner { get; set; }

        public ICollection<UserListMember> UserListMembers { get; set; }
    }

    public class UserListMember
    {
        [Key]
        public string UserListId { get; set; }

        [Key]
        public string MemberId { get; set; }

        public UserList UserList { get; set; }

        public User Member { get; set; }
    }

    public class UserDbContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<UserList> UserLists { get; set; }
        public DbSet<UserListMember> UserListMembers { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Data Source=EFCoreTest-ExtraAK.db");
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.Entity<UserListMember>(userListMember =>
            {
                userListMember.HasKey(ulm => new { ulm.UserListId, ulm.MemberId });
            });
        }
    }
}
  1. Run the following commands:
dotnet build
dotnet ef migrations add initial
dotnet ef database update
  1. Delete the Migrations folder
  2. Replace the content in the .cs file created in Step 2 with:
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;

namespace EFCoreTest
{
    public class User
    {
        [Key]
        public string Id { get; set; }

        public ICollection<List> Lists { get; set; }

        public ICollection<ListMember> ListMembers { get; set; }
    }

    public class List
    {
        [Key]
        public string Id { get; set; }

        [Required]
        public string OwnerId { get; set; }

        public User Owner { get; set; }

        public ICollection<ListMember> ListMembers { get; set; }
    }

    public class ListMember
    {
        [Key]
        public string ListId { get; set; }

        [Key]
        public string MemberId { get; set; }

        public List List { get; set; }

        public User Member { get; set; }
    }

    public class UserDbContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<List> Lists { get; set; }
        public DbSet<ListMember> ListMembers { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Data Source=EFCoreTest.db");
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.Entity<ListMember>(listMember =>
            {
                listMember.HasKey(ulm => new { ulm.ListId, ulm.MemberId });
            });
        }
    }
}
  1. Repeat the commands in Step 3
  2. Compare the constraints of UserListMembers in EFCoreTest-ExtraAK.db and those of ListMembers in EFCoreTest.db

Further technical details

EF Core version: 2.1.4
Database Provider: Microsoft.EntityFrameworkCore.Sqlite, Npgsql.EntityFrameworkCore.PostgreSQL
Operating system: Windows 10 1803
IDE: Visual Studio Code 1.28.1

@AndriySvyryd
Copy link
Member

Probably a duplicate of #12414

@ajcvickers ajcvickers modified the milestones: 3.0.0, Backlog Jun 28, 2019
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 28, 2019
@AndriySvyryd AndriySvyryd modified the milestones: Backlog, 3.1.0 Aug 28, 2019
@AndriySvyryd AndriySvyryd removed their assignment Aug 28, 2019
AndriySvyryd added a commit that referenced this issue Aug 28, 2019
Don't try to scaffold alternate keys for owned types

Fixes #15698
Fixes #13628
Fixes #12414
AndriySvyryd added a commit that referenced this issue Aug 29, 2019
Don't try to scaffold alternate keys for owned types

Fixes #15698
Fixes #13628
Fixes #12414
@ajcvickers ajcvickers modified the milestones: 3.1.0, 3.1.0-preview1 Oct 15, 2019
@ajcvickers ajcvickers modified the milestones: 3.1.0-preview1, 3.1.0 Dec 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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 type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants