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

Generated migration code doesn't compile when combining owned entities with base types. #13945

Closed
nphmuller opened this issue Nov 13, 2018 · 1 comment

Comments

@nphmuller
Copy link

I believe this scenario is not supported. It would be more clear if a user friendly error message got shown during the migration creation instead of this vague compile error.

Repro:

    public class MyContext : DbContext
    {

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<LessThanFriend>().OwnsOne(e => e.Address);
            modelBuilder.Entity<FullAddress>().HasBaseType<CityAddress>();
        }
    }

    public class LessThanFriend
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public CityAddress Address { get; set; }
    }

    public class CityAddress
    {
        public string Cap { get; set; }
        public string City { get; set; }
    }

    public class FullAddress : CityAddress
    {
        public string Street { get; set; }
    }
  • Run dotnet ef migrations add Initial
  • Run dotnet build.

The following compile error is thrown:

Migrations\MyContextModelSnapshot.cs(52,29): error CS1929: 'ReferenceOwnershipBuilder' does not contain a definition for 'HasDiscriminator' and the best extension method overload 'RelationalEntityTypeBuilderExtensions.HasDiscriminator<string>(EntityTypeBuilder, string)' requires a receiver of type 'EntityTypeBuilder' [C:\Users\mulle\source\repos\ConsoleApp10\ConsoleApp10\ConsoleApp10.csproj]
Migrations\20181113112638_initial.Designer.cs(54,29): error CS1929: 'ReferenceOwnershipBuilder' does not contain a definition for 'HasDiscriminator' and the best extension method overload 'RelationalEntityTypeBuilderExtensions.HasDiscriminator<string>(EntityTypeBuilder, string)' requires a receiver of type 'EntityTypeBuilder' [C:\Users\mulle\source\repos\ConsoleApp10\ConsoleApp10\ConsoleApp10.csproj]

This is the part of the generated code that causes the compile error:

modelBuilder.Entity("ConsoleApp10.LessThanFriend", b =>
                {
                    b.OwnsOne("ConsoleApp10.CityAddress", "Address", b1 =>
                        {
                            b1.HasDiscriminator<string>("Discriminator").HasValue("CityAddress");
                        });
                });

Basically b1 is of type ReferenceOwnershipBuilder. This type doesn't contain the method HasDiscriminator<>().

Further technical details

EF Core packages:

    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.4" />

Operating system: Windows 10 1809
IDE: VS 15.8.9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants