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

'CollectionOwnershipBuilder' does not contain a definition for 'HasAlternateKey' #15698

Closed
WhitWaldo opened this issue May 12, 2019 · 1 comment · Fixed by #17481
Closed

'CollectionOwnershipBuilder' does not contain a definition for 'HasAlternateKey' #15698

WhitWaldo opened this issue May 12, 2019 · 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

@WhitWaldo
Copy link

WhitWaldo commented May 12, 2019

Describe what is not working as expected.
I've got several DbContexts in my solution. All are working as expected except for one, which is generating anomalous code in the snapshot and designer classes. I have an entity, let's call it ABC. ABC has a list of DEFs. DEF is marked as [Owned] and I have it configured as an OwnsMany in the OnModelCreating as in the following:

modelBuilder.Entity<ABC>().OwnsMany(a => a.defs, a => 
{
  a.HasForeignKey(b => b.AbcId);
  a.Property(b => b.Id).HasDefaultValueSql("newid()");
  a.HasKey(b => new {
    b.AbcId,
    b.Id
  });
});

The IDs for each are GUIDs, in case that's relevant. When I run the Add-Migration command, I use the following:

Add-Migration Initial -StartupProject SampleBuilder -Project Data.Abc -Verbose

This runs without issue and produces _Initial.cs, _Initial.Designer.cs and MyContextModelSnapshot. There is nothing flagged as wrong in _Initial.cs, but when looking at _Initial.Designer.cs, I see the following (names changed, of course):

modelBuilder.Entity("ABC", b => {
  //...
  b.OwnsMany("DEF", "DEFs", b1 => {
    b1.Property<Guid>("AbcId");
    b1.Property<Guid>("Id")
      .ValueGeneratedOnAdd()
      .HasDefaultValueSql("newid()");
    b.Property<Guid>("GhiId");
    b.HasKey("AbcId", "Id");
    b.HasAlternateKey("Id"); //When attempting to build or update the database, I get an error that "CollectionOwnershipBuilder" does not contain a definition for "HasAlternateKey".
    b.ToTable("DefTable");
    b.HasOne("ABC")
      .WithMany("Defs")
      .HasForeignKey("AbcId")
      .OnDelete(DeleteBehavior.Cascade);
  });

  //...
});

I see exactly the same thing in the MyContextModelSnapshot.cs file and the same error.

I attempted to remove the migration and that worked fine. I re-added it and the same thing happened. It only occurs in that one spot and I haven't experienced this in any of the other many owned types in this or other DbContexts.

If you are seeing an exception, include the full exceptions details (message and stack trace).

CS1061	'CollectionOwnershipBuilder' does not contain a definition for 'HasAlternateKey' and no accessible extension method 'HasAlternateKey' accepting a first argument of type 'CollectionOwnershipBuilder' could be found (are you missing a using directive or an assembly reference?)	Data.Abc	20190512195516_Initial.Designer.cs	432	Active
CS1061	'CollectionOwnershipBuilder' does not contain a definition for 'HasAlternateKey' and no accessible extension method 'HasAlternateKey' accepting a first argument of type 'CollectionOwnershipBuilder' could be found (are you missing a using directive or an assembly reference?)	Data.Abc	MyContextModelSnapshot.cs	430	Active

Here are the classes and their relevant bits:

public class ABC {
  public ABC(//Lots of parameters) {
    //Lots of property setting
  }

  private ABC(){}

  [Key]
  [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  public Guid Id {get;set;}

  public List<DEF> DEFs {get;set;} = new List<DEF>();
}

[Owned]
public class DEF {
  [Key]
  [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  public Guid Id {get;set;}

  public Guid AbcId {get;set;}
}

Further technical details

EF Core version: 2.2.4
Microsoft.EntityFrameworkCore.Design: 2.2.4
Database Provider: Microsoft.EntityFrameworkCore.SqlServer: 2.2.4
Operating system: Windows 10 x64 Pro
IDE: Visual Studio 2019 Pro Stable channel, 16.0.3

@WhitWaldo
Copy link
Author

Of course, all of 5 minutes after I wrote this up and posted it, I discovered why this was happening, but I still think it should be attributed to buggy behavior.

Shouldn't use the two attributes on the key in the Owned entity. Putting [Key] and [DatabaseGenerated] on the Id introduces this issue for each owned entity you add them to.

Instead, of simply building a migration that can't be used, I'd instead expect to see the migration creation fail indicating that you can't use the Key attribute on OwnsMany subjects (maybe yet) and that the key(s) in the relationship should only be specified in OnModelBuilder.

@ajcvickers ajcvickers added this to the 3.0.0 milestone May 17, 2019
@bricelam bricelam added the verify-fixed This issue is likely fixed in new query pipeline. label Jun 20, 2019
@bricelam bricelam removed their assignment Jun 20, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0, Backlog Jun 28, 2019
@AndriySvyryd AndriySvyryd added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed verify-fixed This issue is likely fixed in new query pipeline. labels 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.

4 participants